Phasor Diagrams and Z-Plane Plots with Pikchr

I thought it might be nice to have some rectangular and polar graph paper for studying pole-zero plots by hand on the Z-plane, so I whipped this up in Pikchr:

# Draw a unit circle with both rectangular and polar grids.
dim = 4

Origin: dot invisible

# By default, Pikchr uses a polar coordinate system where 0 degrees
# points up and positive angles rotate clockwise.  This macro uses the
# standard reference angle and direction for trigonometry, with 0
# degrees pointing to the right and positive angles rotating
# counter-clockwise.
define mathangle { (90 - ($1)) }

define xgrid {
    line from dim / 2 left of Origin + (0, $1) right dim thin thin thin color grey
}

define ygrid {
    line from dim / 2 below   Origin + ($1, 0) up    dim thin thin thin color grey
}

define spoke {
    line from Origin then go 0.05 heading mathangle($1) invisible
    line from last line.end then go 1.95 heading mathangle($1) thin thin thin thin thin thin
    # line from Origin then go 1 heading mathangle($1) thin thin thin thin thin thin
}

define circ {
    circle radius $1 at Origin thin thin thin thin thin thin
}


xgrid(+2.00); xgrid(+1.75); xgrid(+1.50); xgrid(+1.25); xgrid(+1.00); xgrid(+0.75); xgrid(+0.50); xgrid(+0.25)
xgrid(-2.00); xgrid(-1.75); xgrid(-1.50); xgrid(-1.25); xgrid(-1.00); xgrid(-0.75); xgrid(-0.50); xgrid(-0.25)

ygrid(+2.00); ygrid(+1.75); ygrid(+1.50); ygrid(+1.25); ygrid(+1.00); ygrid(+0.75); ygrid(+0.50); ygrid(+0.25)
ygrid(-2.00); ygrid(-1.75); ygrid(-1.50); ygrid(-1.25); ygrid(-1.00); ygrid(-0.75); ygrid(-0.50); ygrid(-0.25)

spoke(15+000); spoke(30+000); spoke(45+000); spoke(60+000); spoke(75+000); spoke(90+000);
spoke(15+090); spoke(30+090); spoke(45+090); spoke(60+090); spoke(75+090); spoke(90+090);
spoke(15+180); spoke(30+180); spoke(45+180); spoke(60+180); spoke(75+180); spoke(90+180);
spoke(15+270); spoke(30+270); spoke(45+270); spoke(60+270); spoke(75+270); spoke(90+270);

circ(0.25); circ(0.50); circ(0.75);
circ(1.25); circ(1.50); circ(1.75);
circ(2.00);


Xaxis:  arrow <-> from dim / 2 left of Origin right dim
text "Re" big bold
Yaxis:  arrow <-> from dim / 2 below   Origin up    dim
text "Im" big bold
UnitC:  circle radius 1 at Origin thin

(source code)

I was pleasantly surpised to learn that Pikchr supports macros. It's only documented in the language grammar, but it works well to simplify diagram source code and extend the little language.

And for when I want to make SVG files for phasor diagrams there's this:

# Draw a unit circle with I/Q labels
Origin: dot
Xaxis:  arrow <-> from 1.25 left of Origin right 2.5
text "I" big bold
Yaxis:  arrow <-> from 1.25 below   Origin up    2.5
text "Q" big bold
UnitC:  circle radius 1 at Origin thin

# By default, Pikchr uses a polar coordinate system where 0 degrees
# points up and positive angles rotate clockwise.  This macro uses the
# standard reference angle and direction for trigonometry, with 0
# degrees pointing to the right and positive angles rotating
# counter-clockwise.
define mathangle { (90 - ($1)) }

# Draw a phasor with a given angle and color.
define phasor {
    arrow from Origin go 1 heading mathangle($1) color $2
    dot at last.end color last.color
    # Fun times: "down" and "left" work correctly even if "until even
    # with" points up or right.
    line from last dot down until even with Xaxis thin color last.color dashed
    line from last dot left until even with Yaxis thin color last.color dashed
}

phasor( 20, red)
phasor( 65, green)
phasor(110, blue)

(source code)


Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.

© Copyright 2024, Remington Furman

blog@remcycles.net

@remcycles@subdued.social