1
2
3
4
5
6
7 package net.sourceforge.kamiwaai;
8
9 import java.awt.Graphics;
10 import java.awt.Graphics2D;
11 import java.awt.Point;
12 import java.awt.event.MouseEvent;
13 import java.util.StringTokenizer;
14
15 import net.sourceforge.kamiwaai.geometricalgebra.Line;
16 import net.sourceforge.kamiwaai.geometricalgebra.MultiVector;
17 import net.sourceforge.kamiwaai.geometricalgebra.PointC;
18 import net.sourceforge.kamiwaai.geometricalgebra.SwingDrawable;
19
20 class CMeineCanvas extends KamiWaAiCanvas {
21 public CMeineCanvas(KamiWaAi ai) {
22 super(ai);
23 }
24
25 public void mousePressed(MouseEvent e) {
26
27
28 aktuell = kamiwaai.ModeChoice.getSelection();
29
30
31
32
33 if (aktuell == null)
34 return;
35
36 label = aktuell.getActionCommand();
37
38
39
40 if (label.equals("point")) {
41 pointMode(e);
42 }
43
44 if (label.equals("move")) {
45 moveMode(e);
46 }
47
48 if (label.equals("circle")) {
49 circleMode(e);
50
51 }
52
53 if (label.equals("sphere")) {
54 sphereMode(e);
55
56 }
57
58 if (label.equals("sphere C,r")) {
59 sphereRadiusMode(e);
60 }
61
62 if (label.equals("line")) {
63 lineMode(e);
64
65 }
66
67 if (label.equals("S int l")) {
68 sphereIntersectLine(e);
69 }
70
71 if (label.equals("S int S")) {
72 sphereIntersectSphere(e);
73 }
74
75 }
76
77 /***
78 * @param e
79 */
80 private void moveMode(MouseEvent e) {
81 Point pt = e.getPoint();
82 this.kamiwaai.selectedPoint = pickPoint(pt);
83 this.kamiwaai.rdlist.clear();
84 }
85
86 /***
87 * @param e
88 */
89 private void pointMode(MouseEvent e) {
90 if (kamiwaai.sideview == false) {
91 k = 0;
92 PointC ptc = new PointC();
93 ptc.sete1(e.getX());
94 ptc.sete2(e.getY());
95 ptc.draw(this.kamiwaai.m_malflaeche.getGraphics());
96 this.kamiwaai.pnum = this.kamiwaai.pnum + 1;
97 String key = "P" + String.valueOf(this.kamiwaai.pnum);
98 this.kamiwaai.ENames.put(key, ptc);
99 kamiwaai.sideview = true;
100 }
101 }
102
103 public void mouseDragged(MouseEvent e) {
104
105
106 aktuell = kamiwaai.ModeChoice.getSelection();
107
108
109
110
111 if (aktuell == null)
112 return;
113
114 label = aktuell.getActionCommand();
115
116
117
118 if (label.equals("move")) {
119 if (this.kamiwaai.selectedPoint != " ") {
120
121 PointC movingPt = (PointC) this.kamiwaai.ENames
122 .get(this.kamiwaai.selectedPoint);
123 movingPt.erase(this.kamiwaai.m_malflaeche.getGraphics());
124 movingPt.eraseZ(this.kamiwaai.m_malflaecheZ.getGraphics());
125 movingPt.sete1(e.getX());
126 movingPt.sete2(e.getY());
127 this.kamiwaai.ENames.put(this.kamiwaai.selectedPoint, movingPt);
128 this.kamiwaai.mvdPoint = this.kamiwaai.selectedPoint;
129 this.kamiwaai.rdlist.add(this.kamiwaai.mvdPoint);
130
131 redrawPoints();
132 }
133
134 }
135
136 }
137
138 public void paint(Graphics gF) {
139
140 Graphics2D g = (Graphics2D) gF;
141 g.clearRect(0, 0, 300, 300);
142
143
144
145
146 for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
147 StringTokenizer st = new StringTokenizer(
148 (String) this.kamiwaai.Clist.get(i), ",");
149 String element = st.nextToken();
150 SwingDrawable renewObject = (SwingDrawable) this.kamiwaai.ENames
151 .get(element);
152 renewObject.draw(g);
153 }
154 }
155
156 /***
157 * Return key of point that contains pt, if any, or " "otherwise.
158 */
159 String pickPoint(Point pt) {
160 double x = getXCoordinate(pt);
161 double y = getYCoordinate(pt);
162
163 for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
164 StringTokenizer st = new StringTokenizer(
165 (String) this.kamiwaai.Clist.get(i), ",");
166 String element = st.nextToken();
167 if (element.startsWith("P")) {
168 if (((PointC) this.kamiwaai.ENames.get(element))
169 .isXYpoint(x, y))
170 return element;
171 }
172 }
173 return " ";
174 }
175
176 /***
177 * Return key of the line that contains pt, if any, or " " otherwise.
178 *
179 */
180 protected String pickLine(Point pt) {
181 double[] picker = new double[3];
182 double delta = 3.0;
183 String linekey = " ";
184 picker[0] = pt.getX();
185 picker[1] = getYCoordinate(pt);
186 picker[2] = 0.0;
187
188 for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
189 StringTokenizer st = new StringTokenizer(
190 (String) this.kamiwaai.Clist.get(i), ",");
191 String element = st.nextToken();
192 if (element.startsWith("L")) {
193 Line oneline = (Line) this.kamiwaai.ENames.get(element);
194 double dxy = oneline.distanceXY(picker);
195 if (dxy <= delta) {
196 linekey = element;
197 delta = dxy;
198 }
199 }
200 }
201 return linekey;
202 }
203
204 /***
205 * @param pt
206 * @param Poles
207 * @return
208 */
209 public double getDPoles(Point pt, MultiVector Poles) {
210 return Math.sqrt((getXCoordinate(pt) - getPolesX(Poles))
211 * (getXCoordinate(pt) - getPolesX(Poles))
212 + (getYCoordinate(pt) - getPolesY(Poles))
213 * (getYCoordinate(pt) - getPolesY(Poles)));
214 }
215
216 /***
217 * @param pt
218 * @param Pole
219 * @return
220 */
221 public double getDPole(Point pt, MultiVector Pole) {
222 return Math.sqrt((getXCoordinate(pt) - getPoleX(Pole))
223 * (getXCoordinate(pt) - getPoleX(Pole))
224 + (getYCoordinate(pt) - getPoleY(Pole))
225 * (getYCoordinate(pt) - getPoleY(Pole)));
226 }
227 }