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  // It the personal wish of E.M.S. Hitzer,
28  // that you never use this software for infringing human dignity:
29  //
30  // "God created man in his own image,
31  // in the image of God he created him;
32  // male and female he created them.
33  // [The Bible, Genesis chapter 1 verse 27]
34  //
35  //
36  //
37  // File Line.java
38  // 2003-5-1
39  package net.sourceforge.kamiwaai.geometricalgebra;
40  import java.awt.BasicStroke;
41  import java.awt.Color;
42  import java.awt.Graphics;
43  import java.awt.Graphics2D;
44  import java.awt.geom.GeneralPath;
45  public class Line extends GeometricObject {
46  	//private MultiVector[] basepoints = new MultiVector[2];
47  	// The default color of lines will be cyan.
48  	private Color color = Color.cyan;
49  	public Line(MultiVector L2) {
50  		L = L2;
51  	}
52  	public Line(PointC point1, PointC point2) {
53  		MultiVector point1MV = point1.getMultiVector();
54  		MultiVector point2MV = point2.getMultiVector();
55  		L = point1MV.OutProd(point2MV).OutProd(MultiVectors.n());
56  	}
57  	public void setColor(Color c) {
58  		color = c;
59  	}
60  	public MultiVector getMoment() {
61  		MultiVector m = L.OutProd(nbar).mult(N);
62  		return m;
63  	}
64  	public MultiVector getLineVector() {
65  		MultiVector lv = L.Rcontract(N);
66  		return lv;
67  	}
68  	public MultiVector LinePoint(double alpha) {
69  		// The returned result of this method is only the 3D Euclidean part of
70  		// the
71  		// full conformal point in MultiVector form!
72  		MultiVector u = getLineVector();
73  		MultiVector uinv = u.multSc(1.0 / (u.magnitude() * u.magnitude()));
74  		MultiVector X = getMoment().mult(uinv).add(u.multSc(alpha));
75  		return X;
76  	}
77  	public double[] linepoint3D(double alpha) {
78  		double[] lps = new double[3];
79  		lps[0] = LinePoint(alpha).get3DEuclidianPoint()[0];
80  		lps[1] = LinePoint(alpha).get3DEuclidianPoint()[1];
81  		lps[2] = LinePoint(alpha).get3DEuclidianPoint()[2];
82  		return lps;
83  	}
84  	public void draw(Graphics g) {
85  		//Line line = new Line(L);
86  		double par = 5000.0 / getLineVector().magnitude();
87  		Graphics2D g2 = (Graphics2D) g;
88  		g2.setColor(color);
89  		g2.setStroke(new BasicStroke(1.0f));
90  		// Create a line using a general path object
91  		GeneralPath p = new GeneralPath();
92  		p.moveTo((float) linepoint3D(-par)[0], (float) linepoint3D(-par)[1]);
93  		p.lineTo((float) linepoint3D(par)[0], (float) linepoint3D(par)[1]);
94  		// render the line's path
95  		g2.draw(p);
96  	}
97  	public void drawZ(Graphics g) {
98  		//Line line = new Line(L);
99  		double par = 5000.0 / getLineVector().magnitude();
100 		Graphics2D g2 = (Graphics2D) g;
101 		g2.setColor(color);
102 		g2.setStroke(new BasicStroke(1.0f));
103 		// Create a line using a general path object
104 		GeneralPath p = new GeneralPath();
105 		p.moveTo((float) linepoint3D(-par)[2], (float) linepoint3D(-par)[1]);
106 		p.lineTo((float) linepoint3D(par)[2], (float) linepoint3D(par)[1]);
107 		// render the line's path
108 		g2.draw(p);
109 	}
110 //	public void setBasePoint(MultiVector basepoint, int p12) {
111 //		if (p12 != 0 || p12 != 1)
112 //			throw new IllegalArgumentException("index should be zero or one");
113 //		basepoints[p12] = basepoint;
114 //	}
115 	
116 	// This method is specific for remaking a circle when one of its
117 	// basepoints is shifted!
118 	//trying to make it immutable
119 	//   public void remake(MultiVector movedBP, int bpno)
120 	//     {
121 	//      basepoints[bpno] = movedBP;
122 	//      MultiVector Lnew = basepoints[0].OutProd(basepoints[1]).OutProd(n);
123 	//      L = Lnew;
124 	//     }
125 //	public MultiVector PDistance(double[] picked) {
126 //		MultiVector P, XP, u, uinv, distance;
127 //		Line line = new Line(L);
128 //		P = e1.multSc(picked[0]).add(e2.multSc(picked[1])).add(
129 //				e3.multSc(picked[2]));
130 //		XP = P.sub(line.LinePoint(0.0));
131 //		u = line.getLineVector();
132 //		uinv = u.multSc(1.0 / (u.magnitude() * u.magnitude()));
133 //		distance = (XP.OutProd(u)).mult(uinv);
134 //		return distance;
135 //	}
136 	public MultiVector PDistance2D(double[] point, MultiVector plane) {
137 		// There should be a conformal way to determine the distance of a line
138 		// and a point! Look for it.
139 		// Euc3Vector class use eliminated on 2003-5-1!
140 		MultiVector Lp, Pp, x, Tx, Txr, XPp, xpp, up, upinv, dp;
141 		Lp = L.Lcontract(plane).mult(plane.multSc(-1.0));
142 		Line linep = new Line(Lp);
143 		PointC Pc = new PointC();
144 		Pc.sete1(point[0]);
145 		Pc.sete2(point[1]);
146 		Pc.sete3(point[2]);
147 		Pp = Pc.getMultiVector().Lcontract(plane).mult(plane.multSc(-1.0));
148 		x = linep.LinePoint(0.0);
149 		Tx = One.add(n.mult(x).multSc(0.5));
150 		Txr = Tx.reverse();
151 		XPp = Txr.mult(Pp).mult(Tx);
152 		xpp = XPp.OutProd(N).mult(N); // 3D space part as MultiVector.
153 		up = linep.getLineVector(); // Here the 2D vector as MultiVector, not
154 		// conformal!
155 		upinv = up.multSc(1.0 / (up.magnitude() * up.magnitude()));
156 		dp = (xpp.OutProd(up)).mult(upinv);
157 		return dp;
158 	}
159 	public double distanceXY(double[] point) {
160 		MultiVector xyplane, dxy;
161 		xyplane = e1.OutProd(e2).mult(N);
162 		dxy = PDistance2D(point, xyplane);
163 		return dxy.magnitude();
164 	}
165 	public double distanceYZ(double[] point) {
166 		MultiVector yzplane, dyz;
167 		yzplane = e2.OutProd(e3).mult(N);
168 		dyz = PDistance2D(point, yzplane);
169 		return dyz.magnitude();
170 	}
171 
172 }