// // this shows how to rotate a line around a center // without using the built-in rotate() function. // // no copyright, january 2007, benn:org // // set start roation angle here int angle = 20; // set start and end point of line int x1 = 50; int y1 = 150; int x2 = 150; int y2 = 150; // set roation center int xCenter = 100; int yCenter = 100; // declare variables for later use float radian, x1R, y1R, x2R, y2R; void setup() { size(200, 200); // set screen size frameRate(25); // set frame rate smooth(); // anti-aliasing on } void draw() { background(255); // clear screen // draw unrotated line in blue stroke(0,0,255); line(x1, y1, x2, y2); // increase rotation angle at every frame and reset if // greater than 360 angle += 10; if (angle > 360) { angle = 0; } // convert degrees to radian radian = angle * PI/180; // these two magic lines rotate the start point. this formula // can easily be ported to other programming languages x1R = xCenter + cos(radian) * (x1 - xCenter) - sin(radian) * (y1 - yCenter); y1R = yCenter + sin(radian) * (x1 - xCenter) + cos(radian) * (y1 - yCenter); // the same for the second coordinate x2R = xCenter + cos(radian) * (x2 - xCenter) - sin(radian) * (y2 - yCenter); y2R = yCenter + sin(radian) * (x2 - xCenter) + cos(radian) * (y2 - yCenter); // draw rotated line in red stroke(255,0,0); line(x1R, y1R, x2R, y2R); }