added resistor and capacitor

This commit is contained in:
Louis Heredero 2024-11-09 23:03:44 +01:00
parent e1e561bb6c
commit cc8478a3c7
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
3 changed files with 150 additions and 0 deletions

View File

@ -12,4 +12,7 @@
#import "elements/logic/xor.typ": gate-xor, gate-xnor #import "elements/logic/xor.typ": gate-xor, gate-xnor
#import "elements/logic/buf.typ": gate-buf, gate-not #import "elements/logic/buf.typ": gate-buf, gate-not
#import "elements/electrical/resistor.typ": resistor
#import "elements/electrical/capacitor.typ": capacitor
#import "elements/group.typ": group #import "elements/group.typ": group

View File

@ -0,0 +1,52 @@
#import "@preview/cetz:0.2.2": draw
#import "../element.typ"
#import "../ports.typ": add-port
#let draw-shape(id, tl, tr, br, bl, fill, stroke) = {
let f = draw.rect(tl, br, name: id, stroke: stroke, fill: fill)
return (f, tl, tr, br, bl)
}
/// Draws a capacitor
///
/// #examples.capacitor
/// For other parameters description, see #doc-ref("element.elmt")
/// - vertical (bool): Whether the element is vertical or horizontal. If false, port 0 is placed on the west side and port 1 on the east. If true, they are on the north, respectively the south sides
#let capacitor(
x: none,
y: none,
w: none,
h: none,
name: none,
name-anchor: "center",
vertical: false,
fill: none,
stroke: black + 1pt,
id: "",
debug: (
ports: false
)
) = {
let ports = if vertical {(
north: ((id: "0"),),
south: ((id: "1"),)
)} else {(
west: ((id: "0"),),
east: ((id: "1"),)
)}
element.elmt(
draw-shape: draw-shape.with(),
x: x,
y: y,
w: w,
h: h,
name: name,
name-anchor: name-anchor,
ports: ports,
fill: fill,
stroke: stroke,
id: id,
debug: debug
)
}

View File

@ -0,0 +1,95 @@
#import "@preview/cetz:0.2.2": draw
#import "../element.typ"
#import "../ports.typ": add-port
#let draw-shape(
id, tl, tr, br, bl,
fill, stroke,
zigzags: 7,
vertical: false
) = {
let (x, y) = bl
let (width, height) = (tr.at(0) - x, tr.at(1) - y)
let pts = if vertical {(
(x + width / 2, y),
(x + width / 2, y + height * 0.2),
)} else {(
(x, y + height / 2),
(x + width * 0.2, y + height / 2),
)}
for i in range(zigzags) {
let r = ((i+0.5) / zigzags * 0.6 + 0.2)
let pos = if vertical {(
x + width * calc.rem(i, 2),
r * height + y
)} else {(
r * width + x,
y + height * calc.rem(i, 2)
)}
pts.push(pos)
}
pts += if vertical {(
(x + width / 2, y + height * 0.8),
(x + width / 2, y + height),
)} else {(
(x + width * 0.8, y + height / 2),
(x + width, y + height / 2),
)}
let f = draw.group(name: id, {
draw.line(..pts, stroke: stroke)
})
return (f, tl, tr, br, bl)
}
/// Draws a resistor
///
/// #examples.resistor
/// For other parameters description, see #doc-ref("element.elmt")
/// - vertical (bool): Whether the element is vertical or horizontal. If false, port 0 is placed on the west side and port 1 on the east. If true, they are on the north, respectively the south sides
/// - zigzags (number): Number of zigzags to draw
#let resistor(
x: none,
y: none,
w: none,
h: none,
name: none,
name-anchor: "center",
vertical: false,
zigzags: 7,
fill: none,
stroke: black + 1pt,
id: "",
debug: (
ports: false
)
) = {
let ports = if vertical {(
north: ((id: "0"),),
south: ((id: "1"),)
)} else {(
west: ((id: "0"),),
east: ((id: "1"),)
)}
element.elmt(
draw-shape: draw-shape.with(
vertical: vertical,
zigzags: zigzags
),
x: x,
y: y,
w: w,
h: h,
name: name,
name-anchor: name-anchor,
ports: ports,
fill: fill,
stroke: stroke,
id: id,
debug: debug
)
}