/* draws a straight line in red, waits a second, rotates that line, and draw the result in blue near the middle of main (note main is not the last function here) const double line[4] = { 0, 150, 150, 1 }; defines the line to be rotated, it is at a slant of 45 degrees but only to the y and z axes. We can't see in z, so it appears vertical if you change it to const double line[4] = { 150, 150, 0, 1 }; it is not slanting in x and y, so you will see a slant before and after rotation so long as the 4th number is always 1, you can use anything for the x, y, and z coordinates, but if the numbers are too big it will go off the edge of the window if you change BOTH draw_to calls in main into draw_slowly_to, the line doesn't just appear fully formed, you see it being drawn */ #include "library.h" const int winsize = 500; const int halfwinsize = winsize / 2; double matvecmul(const double m[4][4], const double v[4], const int i, const int p) { if (p == 4) return 0.0; return m[i][p] * v[p] + matvecmul(m, v, i, p + 1); } double matvecmul(const double m[4][4], const double v[4], const int i) { return matvecmul(m, v, i, 0); } const double pi = acos(-1.0); double degrees(const double x) { return x * pi / 180.0; } void move_to(const double p[4]) { move_to(halfwinsize + p[0], halfwinsize - p[1]); } void draw_to(const double p[4]) { draw_to(halfwinsize + p[0], halfwinsize - p[1]); } void draw_slowly_to(const double x1, const double y1, const int numsteps); void draw_slowly_to(const double p[4]); void main() { make_window(winsize, winsize); set_pen_width(3); const double rotz[4][4] = { { cos(degrees(30)), sin(degrees(30)), 0, 0 }, { -sin(degrees(30)), cos(degrees(30)), 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; const double centre[4] = { 0, 0, 0, 1 }; const double line[4] = { 0, 150, 150, 1 }; set_pen_color(color::red); move_to(centre); draw_to(line); // change to draw_slowly_to here and below wait(1); const double rotated[4] = { matvecmul(rotz, line, 0), matvecmul(rotz, line, 1), matvecmul(rotz, line, 2), matvecmul(rotz, line, 3) }; set_pen_color(color::blue); move_to(centre); draw_to(rotated); } void draw_slowly_to(const double x1, const double y1, const int numsteps) { if (numsteps == 0) return; const double x0 = get_x_position() - halfwinsize, y0 = halfwinsize - get_y_position(); const double xrange = x1 - x0, yrange = y1 - y0; const double xstep = xrange / numsteps, ystep = yrange / numsteps; const double newx = x0 + xstep, newy = y0 + ystep; draw_to(halfwinsize + newx, halfwinsize - newy); wait(0.01); draw_slowly_to(x1, y1, numsteps - 1); } void draw_slowly_to(const double p[4]) { draw_slowly_to(p[0], p[1], 100); }