// ellipse.java compile javac -cp . ellipse.java // execute java -cp . ellipse // // . x4,y4 minor axis must be perpendicular to major axis // . | . // . | . // x1,y1 .----------------. x2,y2 major axis does not have to be parallel // . | . to the X axis // . | . xc,yc is center // . x3,y3 x,y is point on ellipse public class ellipse { double x1 = -6.0; // first test, easy to check double y1 = 0.0; double x2 = 6.0; double y2 = 0.0; double x3 = 0.0; double y3 = -2.0; double x4 = 0.0; double y4 = 2.0; public ellipse() { double a, b, xc, yc, x, y, chk, t, dt; int nt; int num_points = 0; // for datwrite int num_polys = 0; int poly = 0; int polys[] = new int[1000]; double points[][] = new double[200][3]; System.out.println("ellipse.java running"); System.out.println("x1,y1 ="+x1+","+y1+" one end of major axis"); System.out.println("x2,y2 ="+x2+","+y2+" other end of major axis"); System.out.println("x3,y3 ="+x3+","+y3+" one end of minor axis"); System.out.println("x4,y4 ="+x4+","+y4+" other end of minor axis"); xc = (x1+x2)/2.0; yc = (y3+y4)/2.0; System.out.println("xc,yc ="+xc+","+yc+" center of ellipse"); a = len(x1,y1,x2,y2)/2.0; b = len(x3,y3,x4,y4)/2.0; System.out.println("a = "+a+" length of major axis"); System.out.println("b = "+b+" length of minor axis"); System.out.println("check point x,y on ellipse (x*x)/(a*a) + (y*y)/(b*b) = 1.0"); chk = (x1*x1)/(a*a) + (y1*y1)/(b*b); System.out.println("x1,y1 chk="+chk); chk = (x2*x2)/(a*a) + (y2*y2)/(b*b); System.out.println("x2,y2 chk="+chk); chk = (x3*x3)/(a*a) + (y3*y3)/(b*b); System.out.println("x3,y3 chk="+chk); chk = (x4*x4)/(a*a) + (y4*y4)/(b*b); System.out.println("x4,y4 chk="+chk); System.out.println("given x, y =sqrt((1.0-((x*x)/(a*a))) * (b*b)) = ell(x,a,b)"); System.out.println("generate some points, could be in a loop"); x = (x1); y = ell(x,a,b); System.out.println("point on ellipse x,y = "+x+","+y); x = (x1+x2/8.0); y = ell(x,a,b); System.out.println("point on ellipse x,y = "+x+","+y); x = (x1+x2/4.0); y = ell(x,a,b); System.out.println("point on ellipse x,y = "+x+","+y); x = (x1+x2/2.0); y = ell(x,a,b); System.out.println("point on ellipse x,y = "+x+","+y); x = xc; y = ell(x,a,b); System.out.println("point on ellipse x,y = "+x+","+y); x = (x1/2.0+x2); y = ell(x,a,b); System.out.println("point on ellipse x,y = "+x+","+y); x = (x1/4.0+x2); y = ell(x,a,b); System.out.println("point on ellipse x,y = "+x+","+y); x = (x1/8.0+x2); y = ell(x,a,b); System.out.println("point on ellipse x,y = "+x+","+y); x = (x2); y = ell(x,a,b); System.out.println("point on ellipse x,y = "+x+","+y); System.out.println(" "); System.out.println("use x=xc+a*cos(t) y=yc+b*sin(t) "); for(int i=0; i<200; i++) { points[i][2] = 0.0; // no z } dt = Math.PI/16.0; t = 0.0; nt = 32; points[0][0] = x1; points[0][1] = y1; points[1][0] = x2; points[1][1] = y2; points[2][0] = x3; points[2][1] = y3; points[3][0] = x4; points[3][1] = y4; num_points = 4; polys[poly++] = 2; // major axis polys[poly++] = 1; polys[poly++] = 2; polys[poly++] = 2; // minor axis polys[poly++] = 3; polys[poly++] = 4; for(int i=0; i0) { polys[poly++] = 2; polys[poly++] = num_points-1; polys[poly++] = num_points; } t = t+dt; } polys[poly++] = 2; // close last to first polys[poly++] = num_points; polys[poly++] = 5; num_polys = poly/3; // all lines new datwrite("ellipse.dat", points, num_points, polys, num_polys); System.out.println("ellipse.java wrote allipse.dat"); System.out.println("ellipse.java finished"); } double len(double xa, double ya, double xb, double yb) // length a to b { return Math.sqrt((xa-xb)*(xa-xb) + (ya-yb)*(ya-yb)); } // end len double ell(double x, double a, double b) { return Math.sqrt((1.0-((x*x)/(a*a))) * (b*b)); } // end ell double px(double a, double xc, double t) { return xc+a*Math.cos(t); } // end px double py(double b, double yc, double t) { return yc+b*Math.sin(t); } // end px public static void main (String[] args) { new ellipse(); } } // end ellipse.java