From: bigphil AT solutions DOT mb DOT ca (Mark Phillips) Newsgroups: comp.os.msdos.djgpp Subject: fractal Message-ID: <351ad99b.7246074@news> Lines: 86 Date: Thu, 26 Mar 1998 22:42:49 GMT NNTP-Posting-Host: 207.161.165.50 NNTP-Posting-Date: Thu, 26 Mar 1998 16:42:49 CST To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk I was just playing around trying to write a fractal program using the allegro library. At least as far as I know, it's a fractal, I don't even know the exact definition of what it a fractal, but it involves recursion. I won't bother to try to explain exactly what the program does, but could someone tell me why it won't even compile? I think there is a good chance it has to do with the way I use the allegro stuff. Here is the complete source: (some of the comments may not make sense but they were intended only for me) #include "allegro.h" #include struct point { int x; int y; }; struct line { point p1; point p2; }; class tri { public: point p1; point p2; point p3; void getTri(line); void draw(int); }; void drawOneSide(line, int); int main() { allegro_init(); install_keyboard(); set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0); set_pallete(desktop_pallete); line l1, l2, l3; /***l1.p1=l3.p2 l1.p2=l2.p1 l2.p2=l3.p1***/ l1.p1.x=320; l1.p1.y=2; l1.p2.x=120; l1.p2.y=int(2+sqrt(400*400-200*200)); l2.p1.x=l1.p2.x; l2.p1.y=l1.p2.y; l2.p2.x=520; l2.p2.y=l1.p2.y; l3.p1.x=l2.p2.x; l3.p1.y=l2.p2.y; l3.p2.x=l1.p1.x; l3.p2.y=l1.p1.y; drawOneSide(l1,1); drawOneSide(l2,1); drawOneSide(l3,1); exit(0); } void drawOneSide(line l, int color) { tri triangl; line temp; if(l.p1.x==l.p2.x&&l.p1.y==l.p2.y) putpixel(screen, l.p1.x, l.p1.y, color); else { triangl.getTri(l); triangl.draw(color); color++; if(color>15) color=0; temp.p1.x = l.p1.x; temp.p1.y = l.p1.y; temp.p2.x = triangl.p1.x; temp.p2.y = triangl.p1.y; drawOneSide(temp, color); temp.p1.x = triangl.p1.x; temp.p1.y = triangl.p1.y; temp.p2.x = triangl.p2.x; temp.p2.y = triangl.p2.y; drawOneSide(temp, color); temp.p1.x = triangl.p2.x; temp.p1.y = triangl.p2.y; temp.p2.x = triangl.p3.x; temp.p2.y = triangl.p3.y; drawOneSide(temp, color); temp.p1.x = triangl.p3.x; temp.p1.y = triangl.p3.y; temp.p2.x = l.p2.x; temp.p2.y = l.p2.y; drawOneSide(temp, color); } } void tri::draw(int color) { // triangle(screen, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, color); } void tri::getTri(line l) { //new point = p2 double slope; int length; point d, midpt; // get points 1 and 3 from points on line p1.x = l.p1.x + (l.p2.x - l.p1.x) / 3; p1.y = l.p1.y + (l.p2.y - l.p1.y) / 3; p3.x = l.p1.x + 2*(l.p2.x - l.p1.x) / 3; p3.y = l.p1.y + 2*(l.p2.y - l.p1.y) / 3; // get midpoint of points 1 and 3 midpt.x = p1.x + (p3.x - p1.x) / 2; midpt.y = p1.y + (p3.y - p1.y) / 2; // calculate slope slope = (l.p1.y-l.p2.y)/(l.p1.x-l.p2.x); slope = -1/slope; length = int(sqrt( (l.p1.x-l.p2.x)*(l.p1.x-l.p2.x)+ (l.p1.y-l.p2.y)*(l.p1.y-l.p2.y) )); // d.x is some formula i worked out on paper from the distance formula d.x = int(length/sqrt(1+sqrt(slope))); d.y = int(slope*d.x); // make sure new triangle is sticking out and not into previous if(p1.x>p3.x) d.x = -ABS(d.x); else d.x = ABS(d.x); if(p1.y>p3.y) d.y = -ABS(d.y); else d.y = ABS(d.y); // finally calculate point 2 (more trouble than it's worth) p2.x = midpt.x + d.x; p2.y = midpt.y + d.y; }