use crate::{ layout::{self, CircuitLayout}, share, }; pub fn paint(layout: CircuitLayout) -> Vec { let mut draw_commands = vec![]; for wire in layout.wires() { draw_commands.push(format!( "line(({},{}),({},{}))", wire.from.0, wire.from.1, wire.to.0, wire.to.1 )); } for ele in layout.elements() { match ele { layout::Element::VSource { label, voltage, location, rotation, } => { draw_commands.push(format!( "circle(({},{}), radius: 0.4, fill: white)", location.0, location.1 )); draw_commands.push(format!( "line(({},{}),({},{}))", location.0 as f32 - 0.4, location.1, location.0 as f32 + 0.4, location.1 )); draw_commands.push(format!( "content(({},{}), text(size: 8pt)[{}])", location.0, location.1 as f32 + 0.2, label )); draw_commands.push(format!( "content(({},{}), text(size: 8pt)[{}V])", location.0, location.1 as f32 - 0.2, voltage )); let (dx, dy): (f32, f32) = match rotation { share::Direction::Up => (0.0, 1.0), share::Direction::Down => (0.0, -1.0), share::Direction::Left => (-1.0, 0.0), share::Direction::Right => (1.0, 0.0), }; let (px, py) = (-dy, dx); // perpendicular: 90° counter-clockwise from the direction let (x, y) = (location.0 as f32, location.1 as f32); let side = 0.4 + 0.1; // circle radius + gap: how far the arrow is from the center // center of the arrow, shifted to the side of the circle let (cx, cy) = (x + px * side, y + py * side); draw_commands.push(format!( "mark(({},{}), ({},{}), symbol: \">\", fill: black, scale: 0.5)", cx - dx * 0.35, cy - dy * 0.35, cx - dx, cy - dy, )); draw_commands.push(format!( "line(({},{}), ({},{}))", cx + dx * 0.3, cy + dy * 0.3, cx - dx * 0.2, cy - dy * 0.2, )); } layout::Element::ASource { label, current, location, rotation, } => { draw_commands.push(format!( "circle(({},{}), radius: 0.4, fill: white)", location.0, location.1 )); draw_commands.push(format!( "line(({},{}),({},{}))", location.0, location.1 as f32 - 0.4, location.0, location.1 as f32 + 0.4 )); draw_commands.push(format!( "content(({},{}), text(size: 8pt)[{}])", location.0 as f32 + 0.2, location.1, label )); draw_commands.push(format!( "content(({},{}), text(size: 8pt)[{}A])", location.0 as f32 - 0.2, location.1, current )); let (dx, dy): (f32, f32) = match rotation { share::Direction::Up => (0.0, 1.0), share::Direction::Down => (0.0, -1.0), share::Direction::Left => (-1.0, 0.0), share::Direction::Right => (1.0, 0.0), }; let (px, py) = (-dy, dx); // perpendicular: 90° counter-clockwise from the direction let (x, y) = (location.0 as f32, location.1 as f32); let side = 0.4 + 0.1; // circle radius + gap: how far the arrow is from the center // center of the arrow, shifted to the side of the circle let (cx, cy) = (x + px * side, y + py * side); draw_commands.push(format!( "mark(({},{}), ({},{}), symbol: \">\", fill: black, scale: 0.5)", cx + dx * 0.35, cy + dy * 0.35, cx + dx, cy + dy, )); draw_commands.push(format!( "line(({},{}), ({},{}))", cx + dx * 0.2, cy + dy * 0.2, cx - dx * 0.3, cy - dy * 0.3, )); } layout::Element::Resistor { label, ohm, location, rotation, } => match rotation { share::Direction::Up | share::Direction::Down => { draw_commands.push(format!( "rect(({},{}),({},{}), fill: white)", location.0 as f32 - 0.2, location.1 as f32 - 0.4, location.0 as f32 + 0.2, location.1 as f32 + 0.4 )); draw_commands.push(format!( "content(({},{}),anchor: \"west\", text(size: 8pt)[{}])", location.0 as f32 + 0.25, location.1 as f32 + 0.15, label )); draw_commands.push(format!( "content(({},{}),anchor: \"west\", text(size: 8pt)[{}$Omega$])", location.0 as f32 + 0.25, location.1 as f32 - 0.15, ohm )); } share::Direction::Left | share::Direction::Right => { draw_commands.push(format!( "rect(({},{}),({},{}), fill: white)", location.0 as f32 - 0.4, location.1 as f32 - 0.2, location.0 as f32 + 0.4, location.1 as f32 + 0.2 )); draw_commands.push(format!( "content(({},{}), text(size: 8pt)[{}])", location.0, location.1 as f32 + 0.35, label )); draw_commands.push(format!( "content(({},{}), text(size: 8pt)[{}$Omega$])", location.0, location.1 as f32 - 0.35, ohm )); } }, layout::Element::Node { location } => { draw_commands.push(format!( "rect(({},{}),({},{}), fill: black)", location.0 as f32 - 0.05, location.1 as f32 - 0.05, location.0 as f32 + 0.05, location.1 as f32 + 0.05 )); } layout::Element::Terminal { label, location } => { draw_commands.push(format!( "circle(({},{}), radius: 0.2, fill: white)", location.0, location.1 )); draw_commands.push(format!( "content(({},{}), text(size: 8pt)[{}])", location.0, location.1, label )); } } } draw_commands }