Skip to content

Instantly share code, notes, and snippets.

@vEnhance
Created September 22, 2025 00:23
Show Gist options
  • Select an option

  • Save vEnhance/b5dc1387b125d9aa29426a4171550859 to your computer and use it in GitHub Desktop.

Select an option

Save vEnhance/b5dc1387b125d9aa29426a4171550859 to your computer and use it in GitHub Desktop.
Claude suggested resistor code
import olympiad;
size(600,300);
// Helper function to draw a resistor
void drawResistor(pair A, pair B, string label) {
pair mid = (A + B) / 2;
pair dir = unit(B - A);
pair perp = rotate(90) * dir;
real len = length(B - A);
real resWidth = 0.3;
real zigzag = 0.15;
// Calculate zigzag points
pair start = A + 0.2 * dir * len;
pair end = B - 0.2 * dir * len;
pair[] zigpts;
for(int i = 0; i <= 6; ++i) {
real t = i / 6.0;
pair pt = start + t * (end - start);
if(i % 2 == 1) pt += zigzag * perp;
zigpts.push(pt);
}
// Draw connecting lines and zigzag
draw(A--start);
for(int i = 0; i < zigpts.length - 1; ++i) {
draw(zigpts[i]--zigpts[i+1], linewidth(1.2));
}
draw(end--B);
// Add label
label(label, mid + 0.4 * perp, fontsize(10));
}
// Main connection points
pair P = (0, 0);
pair Q = (13, 0);
// Section 1 junction points
pair A1 = (1, 0);
pair B1 = (3, 0);
// Section 2 junction points
pair A2 = (4, 0);
pair B2 = (8, 0);
// Section 3 junction points
pair A3 = (9, 0);
pair B3 = (11, 0);
// Draw main horizontal line segments
draw(P--A1, linewidth(1));
draw(B1--A2, linewidth(1));
draw(B2--A3, linewidth(1));
draw(B3--Q, linewidth(1));
// Section 1: x ohm parallel with 1/3 ohm
pair S1_top = A1 + (0, 1);
pair S1_bottom = A1 + (0, -1);
pair E1_top = B1 + (0, 1);
pair E1_bottom = B1 + (0, -1);
draw(A1--S1_top--E1_top--B1, linewidth(1));
draw(A1--S1_bottom--E1_bottom--B1, linewidth(1));
drawResistor(S1_top, E1_top, "$x\,\Omega$");
drawResistor(S1_bottom, E1_bottom, "$\frac{1}{3}\,\Omega$");
// Section 2: y ohm parallel with two 1/3 ohm in series
pair S2_top = A2 + (0, 1);
pair S2_bottom = A2 + (0, -1);
pair E2_top = B2 + (0, 1);
pair E2_bottom = B2 + (0, -1);
pair mid2 = (6, -1);
draw(A2--S2_top--E2_top--B2, linewidth(1));
draw(A2--S2_bottom--mid2--E2_bottom--B2, linewidth(1));
drawResistor(S2_top, E2_top, "$y\,\Omega$");
drawResistor(S2_bottom, mid2, "$\frac{1}{3}\,\Omega$");
drawResistor(mid2, E2_bottom, "$\frac{1}{3}\,\Omega$");
// Section 3: z ohm parallel with two 1/3 ohm resistors in parallel
pair S3_top = A3 + (0, 1);
pair S3_mid = A3 + (0, -1);
pair S3_bottom = A3 + (0, -2);
pair E3_top = B3 + (0, 1);
pair E3_mid = B3 + (0, -1);
pair E3_bottom = B3 + (0, -2);
draw(A3--S3_top--E3_top--B3, linewidth(1));
draw(A3--S3_mid, linewidth(1));
draw(S3_mid--S3_bottom--E3_bottom--B3, linewidth(1));
draw(S3_mid--E3_mid--B3, linewidth(1));
drawResistor(S3_top, E3_top, "$z\,\Omega$");
drawResistor(S3_mid, E3_mid, "$\frac{1}{3}\,\Omega$");
drawResistor(S3_bottom, E3_bottom, "$\frac{1}{3}\,\Omega$");
// Add terminal labels
label("$P$", P, W, fontsize(12));
label("$Q$", Q, E, fontsize(12));
// Add terminal dots
dot(P, linewidth(4));
dot(Q, linewidth(4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment