xxxxxxxxxx
60
/*
*/
function setup() {
createCanvas(300, 250);
textSize(16);
}
function draw() {
background(235);
drawHorizResistor(4700, 50, 50, 200, 50); // Example usage
drawHorizResistor(330, 50, 150, 200, 50); // Example usage
}
// Function to draw a horizontal resistor
function drawHorizResistor(resistance, x, y, w, h) {
// Resistor color bands
const colors = {
0: '#000000', // Black
1: '#964B00', // Brown
2: '#FF0000', // Red
3: '#FFA500', // Orange
4: '#FFFF00', // Yellow
5: '#008000', // Green
6: '#0000FF', // Blue
7: '#800080', // Violet
8: '#808080', // Grey
9: '#FFFFFF' // White
};
// Calculate resistor values
let digits = resistance.toString().split('').map(Number);
let firstDigit = digits[0];
let secondDigit = digits[1];
let multiplier = Math.pow(10, digits.length - 2);
// Wire below resistor body
fill('black');
rect(x-20, y+20, w+40, 10);
// Draw resistor body
fill('#D2B48C'); // Tan color
rect(x, y, w, h, 20);
// Draw color bands
let bandWidth = w / 10;
fill(colors[firstDigit]);
rect(x + bandWidth, y, bandWidth, h);
fill(colors[secondDigit]);
rect(x + 3 * bandWidth, y, bandWidth, h);
fill(colors[multiplier.toString().length - 1]);
rect(x + 5 * bandWidth, y, bandWidth, h);
// Draw resistance label
fill(0);
textSize(16);
textAlign(CENTER, CENTER);
text(resistance.toString() + ' Ω', x + w / 2, y - 15);
}