View Javadoc

1   // GeometricAlgebra Java package file
2   // Current version
3   // Go to ... http://sinai.mech.fukui-u.ac.jp/gcj/software/KamiWaAi/index.html
4   // Copyright (C) 2003, Eckhard M.S. Hitzer
5   //
6   // This library is free software; you can redistribute it and/or
7   // modify it under the terms of the GNU Lesser General Public
8   // License as published by the Free Software Foundation; either
9   // version 2.1 of the License, or any later version.
10  //
11  // This library is distributed in the hope that it will be useful,
12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  // Lesser General Public License for more details.
15  //
16  // You should have received a copy of the GNU Lesser General Public
17  // License along with this library; if not, write to the Free Software
18  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  // Go to ... http://www.gnu.org/copyleft/lesser.html
20  // 
21  // hitzer@mech.fukui-u.ac.jp
22  // 
23  // Department of Mechanical Engineering, Fukui University
24  // 3-9-1 Bunkyo, 910-8507 Fukui, Japan
25  // 
26  //
27  //
28  // It the personal wish of E.M.S. Hitzer,
29  // that you never use this software for infringing human dignity:
30  //
31  // "God created man in his own image,
32  // in the image of God he created him;
33  // male and female he created them.
34  // [The Bible, Genesis chapter 1 verse 27]
35  //
36  //
37  //
38  // 
39  // File Circle.java
40  // 2003-5-1
41  package net.sourceforge.kamiwaai.geometricalgebra;
42  import java.awt.BasicStroke;
43  import java.awt.Color;
44  import java.awt.Graphics;
45  import java.awt.Graphics2D;
46  import java.awt.geom.GeneralPath;
47  public class Circle extends GeometricObject{
48  	//	private MultiVector[] basepoints = new MultiVector[3];
49  	// The default color of circles will be red.
50  	private Color color = Color.red;
51  	public Circle(MultiVector L3) {
52  		L = L3;
53  	}
54  	public Circle(PointC point1, PointC point2, PointC point3) {
55  		L = point1.getMultiVector().OutProd(point2.getMultiVector())
56  				.OutProd(point3.getMultiVector());
57  	}
58  	public double radius() {
59  		double r2 = (L.ScProd(L))
60  				* (-1.0 / (n.OutProd(L).ScProd(n.OutProd(L))));
61  		double r = Math.sqrt(r2);
62  		return r;
63  	}
64  	public MultiVector Center() {
65  		// Gives the full conformal MultiVector for the center of the circle
66  		Circle LC = new Circle(L);
67  		MultiVector low = n.OutProd(L).mult(I).multSc(-1.0);
68  		double llow = low.ScProd(low);
69  		MultiVector Low = low.multSc(1.0 / llow); // inverse of low
70  		MultiVector Result = L.mult(I).mult(Low);
71  		double r = LC.radius();
72  		MultiVector Cent = Result.add(n.multSc(0.5 * r * r));
73  		return Cent;
74  	}
75  
76  	public double[] center() {
77  		// Gives the 3D Euclidean vector of the center of the circle
78  		double[] cvector = new double[3];
79  		Circle LC = new Circle(L);
80  		cvector[0] = LC.Center().get3DEuclidianPoint()[0];
81  		cvector[1] = LC.Center().get3DEuclidianPoint()[1];
82  		cvector[2] = LC.Center().get3DEuclidianPoint()[2];
83  		return cvector;
84  	}
85  	public MultiVector Cplane() {
86  		// Gives the 3D Euclidean bivector as part of the conformal algbra
87  		// stripping away the non-Euclidean components
88  		MultiVector ICp = L.OutProd(n).Rcontract(N);
89  		MultiVector IC = ICp.normalize();
90  		return IC;
91  	}
92  	public MultiVector generator(int inc) {
93  		// Gives the Motor (MuliVector that rotates a given
94  		// angle increment 2pi/inc about the circle center)
95  		int UP = inc;
96  		double angle = 2 * Math.PI / UP;
97  		MultiVector cv, IC, Tc, Tcr, R, D;
98  		Circle LC = new Circle(L);
99  		cv = LC.Center().OutProd(N).mult(N); // Euclidean 3D center vector.
100 		// Translation operator
101 		Tc = One.add(n.mult(cv).multSc(0.5));
102 		Tcr = Tc.reverse();
103 		IC = LC.Cplane();
104 		// Rotation rotor R:
105 		R = One.multSc(Math.cos(0.5 * angle)).add(
106 				IC.multSc(Math.sin(0.5 * angle)));
107 		// Operator for rotation about the circle center by angle increment.
108 		D = Tc.mult(R).mult(Tcr);
109 		return D;
110 	}
111 	public MultiVector PonC() {
112 		// Gives back one point on the circle
113 		MultiVector IC, rv, rad, Trad, Tradr, ponc;
114 		double r;
115 		Circle LC = new Circle(L);
116 		IC = LC.Cplane();
117 		rv = e1.Lcontract(IC).add(e2.Lcontract(IC));
118 		r = LC.radius();
119 		rad = rv.multSc(r / (rv.magnitude()));
120 		// Translation operator from the center to a point on the cirlce itself
121 		Trad = One.add(n.mult(rad).multSc(0.5));
122 		Tradr = Trad.reverse();
123 		ponc = Trad.mult(LC.Center()).mult(Tradr);
124 		return ponc;
125 	}
126 	public double[][] cpoints(int inc) {
127 		// Gives back an array of inc Euclidean 3D points on the circle
128 		double[][] points = new double[inc][3];
129 		MultiVector D, Dr, Cpoint;
130 		int f, num;
131 		Circle LC = new Circle(L);
132 		num = inc;
133 		D = LC.generator(num);
134 		Dr = D.reverse();
135 		Cpoint = LC.PonC();
136 		points[0][0] = Cpoint.get3DEuclidianPoint()[0];
137 		points[0][1] = Cpoint.get3DEuclidianPoint()[1];
138 		points[0][2] = Cpoint.get3DEuclidianPoint()[2];
139 		for (f = 1; f < num; f++) {
140 			Cpoint = D.mult(Cpoint).mult(Dr);
141 			// The Euclidean 3D vector points on the circle are:
142 			points[f][0] = Cpoint.get3DEuclidianPoint()[0];
143 			points[f][1] = Cpoint.get3DEuclidianPoint()[1];
144 			points[f][2] = Cpoint.get3DEuclidianPoint()[2];
145 		}
146 		return points;
147 	}
148 	public void setColor(Color c) {
149 		color = c;
150 	}
151 	public void draw(Graphics g) {
152 		int nump = 500;
153 		double[][] CPs;
154 		CPs = cpoints(nump);
155 		Graphics2D g2 = (Graphics2D) g;
156 		g2.setColor(color);
157 		g2.setStroke(new BasicStroke(1.0f));
158 		// Create a circle using a general path object
159 		GeneralPath p = new GeneralPath();
160 		p.moveTo((float) CPs[0][0], (float) CPs[0][1]);
161 		for (int m = 1; m < nump; m++) {
162 			p.lineTo((float) CPs[m][0], (float) CPs[m][1]);
163 		}
164 		p.closePath();
165 		// render the circle's path
166 		g2.draw(p);
167 	}
168 	public void drawZ(Graphics g) {
169 		int nump = 60;
170 		double[][] CPs;
171 		CPs = cpoints(nump);
172 		Graphics2D g2 = (Graphics2D) g;
173 		g2.setColor(color);
174 		g2.setStroke(new BasicStroke(1.0f));
175 		// Create a circle using a general path object
176 		GeneralPath p = new GeneralPath();
177 		p.moveTo((float) CPs[0][2], (float) CPs[0][1]);
178 		for (int m = 1; m < nump; m++) {
179 			p.lineTo((float) CPs[m][2], (float) CPs[m][1]);
180 		}
181 		p.closePath();
182 		// render the circle's path
183 		g2.draw(p);
184 	}
185 
186 }