Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212use crate::{ layout::{self, CircuitLayout}, share,};
pub fn paint(layout: CircuitLayout) -> Vec<String> { 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}