Newsgroups: comp.os.msdos.djgpp Subject: Re: ellipses at an angle Message-ID: <32F60587.49C4@redestb.es> From: Jorge Fernandez Suarez Date: Mon, 03 Feb 1997 16:34:31 +0100 Reply-To: cascoscuro AT redestb DOT es References: <199702030225 DOT UAA22378 AT mail DOT texoma DOT net> Organization: Universidad de Oviedo Nntp-Posting-Host: pcsig4.etsimo.uniovi.es MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------392611895340" Lines: 81 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp This is a multi-part message in MIME format. --------------392611895340 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit > > :Well, you could use the original definition of an ellipse (that is, from > No! This is not even the definition of an ellipse parallel to a coordinate axis! > The general equation for an ellipse is: > > Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0, B^2 - 4*A*C < 0. > > If B == 0, then this reduces to an ellipse parallel to a coordinate axis. well, but you're not giving the formula to rotate one ellipse an angle... this is the conic general formula (i think) The way I've do it it's like this: Ec. of ellipse: x^2/a^2 +y^/b^2 = 1 Ec. of rotate a 2D point: x'= x*cos(angle)-y*sin(angle) y'= x*sin(angle)+y*cos(angle) ... if you plug this 2 formulas into the ellipse ecuation, then you must iterate across the y-axis to retrive the two x coordinate for every "y" and draw one horizontal line between these. Here's my routine... not optimiced at all, btw: --------------392611895340 Content-Type: text/plain; charset=iso-8859-1; name="elipse.c" Content-Transfer-Encoding: 8bit Content-Disposition: inline; filename="elipse.c" void elipse(byte *offset,int cx,int cy,int eje_a,int eje_b,byte theta,byte color) { float a,b,c,f,k1,k2,k3; /* Cts for ecuation resolution */ float aa,bb,cc,dd; /* idem */ float ent,rad; /* real and radikal part */ int y; /* solve ec. for every "y" ... */ int xi,xf; /* ... 2 solutions "xi,xf" */ int yi,yf; /* minimun and maximum of "y" in elipse */ byte *ofs; /* Offset of every Hline */ /* Ec. rotated ellipse --> "A*x^2+ 2*B*x*y + C*y^2 + F= 0 */ /* Rotation (theta) in radians... */ aa = (float)coseno[theta]/128/eje_a; bb = (float) seno[theta]/128/eje_a; cc = (float) seno[theta]/128/eje_b; dd = (float)coseno[theta]/128/eje_b; /* rotated coefficients */ a = aa*aa+cc*cc; b = cc*dd-aa*bb; c = bb*bb+dd*dd; f = -1; /* solutions... x=k1úy ñ û((k1ý+k2)úyý+k3) */ k1 = -b/a; k2 = -c/a; k3 = -f/a; /* min and max will occur when radikal=0 */ yf = sqrt(-k3/(k1*k1+k2))-1; yi = -yf; /* Iteration since min "y" to max for hlines xi-xf */ for(y=yi;y<=yf;y++) { ent = k1*y; /* real part */ rad = sqrt( (k1*k1+k2)*y*y+k3 ); /* radikal part */ xf = ( ent+ rad ); /* Sol. 1 of ec. */ xi = ( ent- rad ); /* Sol. 2 of ec. */ ofs = offset+ANCHO*(cy+y)+cx; /* Hline start */ hline(ofs, xi, xf, 0, color); } } a --------------392611895340--