Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wrapper function to make a radial version of a shape in P5
/*
Radial Shape Wrapper Function
Natalie Freed September 2016
*/
function setup() {
createCanvas(600, 600);
//draw a shape
arrow(width/2+20, height/2, 20, color(0));
//make a radial version
//parameters: shape function, radius, number of shapes, angle of individual shapes, overall rotation angle
var radialArrowOuter = radial(arrow, 200, 20, radians(20), radians(5));
//run the radial version
//parameters are same as arrow: x, y coordinates, width, color
radialArrowOuter(width/2, height/2, 20, color(255, 0, 255, 100));
var radialArrowInner = radial(arrow, 100, 20, radians(20), radians(25));
//run the radial version
radialArrowInner(width/2, height/2, 20, color(255, 0, 255, 100));
}
function draw() {
}
//this function creates a radial version of any shape drawing function
function radial(shape, radius, numShapes, shapeRotation, overallRotation) {
return function() {
var x = arguments[0];
var y = arguments[1];
for (var s = 0; s < numShapes; s++) {
push();
var angle = s * (TWO_PI / numShapes) + overallRotation;
translate(x + radius * cos(angle), y + radius * sin(angle));
rotate(angle + shapeRotation);
arguments[0] = 0;
arguments[1] = 0;
shape.apply(null, arguments);
pop();
}
}
}
//Example arrow shape function by Forrest Green
function arrow(x, y, w, color) {
strokeWeight(2);
stroke(color);
line(x-3.*w,y, x+w, y);
line(x+w,y, x, y-w*0.5);
line(x+w,y, x, y+w*0.5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.