1
2
3
4
5
6
7 package net.sourceforge.kamiwaai;
8
9 import java.awt.Canvas;
10 import java.awt.Color;
11 import java.awt.Dimension;
12 import java.awt.Graphics;
13 import java.awt.Graphics2D;
14 import java.awt.Point;
15 import java.awt.event.MouseEvent;
16 import java.awt.event.MouseListener;
17 import java.awt.event.MouseMotionListener;
18 import java.util.Iterator;
19 import java.util.StringTokenizer;
20 import java.util.Vector;
21
22 import javax.swing.ButtonModel;
23 import javax.swing.JOptionPane;
24
25 import net.sourceforge.kamiwaai.geometricalgebra.Circle;
26 import net.sourceforge.kamiwaai.geometricalgebra.Line;
27 import net.sourceforge.kamiwaai.geometricalgebra.MultiVector;
28 import net.sourceforge.kamiwaai.geometricalgebra.MultiVectors;
29 import net.sourceforge.kamiwaai.geometricalgebra.PointC;
30 import net.sourceforge.kamiwaai.geometricalgebra.Sphere;
31 import net.sourceforge.kamiwaai.geometricalgebra.SwingDrawable;
32
33 /***
34 * @author M. Yamin
35 *
36 * To change the template for this generated type comment go to Window -
37 * Preferences - Java - Code Generation - Code and Comments
38 */
39 public abstract class KamiWaAiCanvas
40 extends Canvas
41 implements MouseListener, MouseMotionListener {
42
43 KamiWaAi kamiwaai;
44 ButtonModel aktuell;
45 String label;
46 private double radius;
47
48
49 int k = 0;
50 KamiWaAiCanvas(KamiWaAi ai) {
51 setBackground(Color.gray);
52 this.kamiwaai = ai;
53 setForeground(Color.red);
54
55 addMouseListener(this);
56 addMouseMotionListener(this);
57
58 }
59 public void update(Graphics g) {
60 paint(g);
61 }
62 public Dimension getMinimumSize() {
63 return new Dimension(300, 300);
64 }
65
66 public Dimension getPreferredSize() {
67 return getMinimumSize();
68 }
69
70 public void mouseMoved(MouseEvent e){}
71 public void mouseClicked(MouseEvent e){}
72 public void mouseExited(MouseEvent e){}
73 public void mouseEntered(MouseEvent e){}
74
75 /***
76 * @param pickPt
77 */
78 private void drawSphereWithCentre(String pickPt) {
79 String CenterPoint = pickPt;
80 System.out.println("center point " + CenterPoint + " chosen!");
81 PointC centre = (PointC) this.kamiwaai.ENames.get(CenterPoint);
82 radius = Double.parseDouble(JOptionPane.showInputDialog(null,
83 "Sphere Radius?"));
84 Sphere S = new Sphere(centre, radius);
85 S.setColor(this.kamiwaai.spherecolor);
86 this.kamiwaai.snum = this.kamiwaai.snum + 1;
87 String key = "S" + String.valueOf(this.kamiwaai.snum);
88 this.kamiwaai.ENames.put(key, S);
89 String Clentry = key + "," + CenterPoint;
90 this.kamiwaai.Clist.add(Clentry);
91 S.drawZ(this.kamiwaai.m_malflaecheZ.getGraphics());
92 S.draw(this.kamiwaai.m_malflaeche.getGraphics());
93 }
94 /***
95 *
96 */
97 private void drawSphere() {
98 PointC sp1 = (PointC) this.kamiwaai.ENames
99 .get(this.kamiwaai.SpherePoints[0]);
100 PointC sp2 = (PointC) this.kamiwaai.ENames
101 .get(this.kamiwaai.SpherePoints[1]);
102 PointC sp3 = (PointC) this.kamiwaai.ENames
103 .get(this.kamiwaai.SpherePoints[2]);
104 PointC sp4 = (PointC) this.kamiwaai.ENames
105 .get(this.kamiwaai.SpherePoints[3]);
106 Sphere S = new Sphere(sp1, sp2, sp3, sp4);
107 S.setColor(this.kamiwaai.spherecolor);
108 this.kamiwaai.snum = this.kamiwaai.snum + 1;
109 String key = "S" + String.valueOf(this.kamiwaai.snum);
110 this.kamiwaai.ENames.put(key, S);
111 String Clentry = key + "," + this.kamiwaai.SpherePoints[0] + ","
112 + this.kamiwaai.SpherePoints[1] + ","
113 + this.kamiwaai.SpherePoints[2] + ","
114 + this.kamiwaai.SpherePoints[3];
115 this.kamiwaai.Clist.add(Clentry);
116 S.draw(this.kamiwaai.m_malflaeche.getGraphics());
117 S.drawZ(this.kamiwaai.m_malflaecheZ.getGraphics());
118 k = 0;
119 }
120 /***
121 *
122 */
123 private void drawCircle() {
124 PointC cp1 = (PointC) this.kamiwaai.ENames
125 .get(this.kamiwaai.CirclePoints[0]);
126 PointC cp2 = (PointC) this.kamiwaai.ENames
127 .get(this.kamiwaai.CirclePoints[1]);
128 PointC cp3 = (PointC) this.kamiwaai.ENames
129 .get(this.kamiwaai.CirclePoints[2]);
130 Circle C = new Circle(cp1, cp2, cp3);
131 C.setColor(this.kamiwaai.circlecolor);
132 this.kamiwaai.cnum = this.kamiwaai.cnum + 1;
133 String key = "C" + String.valueOf(this.kamiwaai.cnum);
134 this.kamiwaai.ENames.put(key, C);
135 String Clentry = key + "," + this.kamiwaai.CirclePoints[0] + ","
136 + this.kamiwaai.CirclePoints[1] + ","
137 + this.kamiwaai.CirclePoints[2];
138 this.kamiwaai.Clist.add(Clentry);
139 C.draw(this.kamiwaai.m_malflaeche.getGraphics());
140 C.drawZ(this.kamiwaai.m_malflaecheZ.getGraphics());
141 k = 0;
142 }
143 public void mouseReleased(MouseEvent e) {
144
145
146
147 aktuell = kamiwaai.ModeChoice.getSelection();
148
149
150
151
152 if (aktuell == null)
153 return;
154
155 label = aktuell.getActionCommand();
156
157
158
159
160
161 if (label.equals("move")) {
162
163
164 if (this.kamiwaai.mvdPoint != " ") {
165 for (Iterator Cit = this.kamiwaai.Clist.iterator(); Cit
166 .hasNext();) {
167 String Citem = (String) Cit.next();
168 StringTokenizer st = new StringTokenizer(Citem, ",");
169 String element = st.nextToken();
170 int constrNumber = st.countTokens();
171
172 String elcomp1 = " ";
173 String elcomp2 = " ";
174 if (constrNumber >= 2) {
175
176 StringTokenizer stcheck = new StringTokenizer(Citem,
177 ",");
178 stcheck.nextToken();
179 elcomp1 = stcheck.nextToken();
180 elcomp2 = stcheck.nextToken();
181 }
182 while (element.startsWith("L") && st.hasMoreTokens()) {
183 String nextt = st.nextToken();
184 if (this.kamiwaai.mvdPoint.equals(nextt)
185 || this.kamiwaai.rdlist.contains(nextt)) {
186
187
188 StringTokenizer stnew = new StringTokenizer(Citem,
189 ",");
190 stnew.nextToken();
191 PointC lp1 = (PointC) this.kamiwaai.ENames
192 .get(stnew.nextToken());
193 PointC lp2 = (PointC) this.kamiwaai.ENames
194 .get(stnew.nextToken());
195 Line newLine = new Line(lp1, lp2);
196 newLine.setColor(this.kamiwaai.linecolor);
197 this.kamiwaai.ENames.put(element, newLine);
198
199 this.kamiwaai.rdlist.add(element);
200 }
201 }
202 while (element.startsWith("C") && constrNumber == 3
203 && st.hasMoreTokens()) {
204 String nextt = st.nextToken();
205 if (this.kamiwaai.mvdPoint.equals(nextt)
206 || this.kamiwaai.rdlist.contains(nextt)) {
207
208
209 StringTokenizer stnew = new StringTokenizer(Citem,
210 ",");
211 stnew.nextToken();
212
213 PointC cp1 = (PointC) this.kamiwaai.ENames
214 .get(stnew.nextToken());
215 PointC cp2 = (PointC) this.kamiwaai.ENames
216 .get(stnew.nextToken());
217 PointC cp3 = (PointC) this.kamiwaai.ENames
218 .get(stnew.nextToken());
219 Circle newCircle = new Circle(cp1, cp2, cp3);
220 newCircle.setColor(this.kamiwaai.circlecolor);
221 this.kamiwaai.ENames.put(element, newCircle);
222
223 this.kamiwaai.rdlist.add(element);
224 }
225 }
226 while (element.startsWith("C") && constrNumber == 2
227 && st.hasMoreTokens()) {
228 String nextt = st.nextToken();
229 if (this.kamiwaai.rdlist.contains(nextt)) {
230
231
232 StringTokenizer stnew = new StringTokenizer(Citem,
233 ",");
234 stnew.nextToken();
235 String sphere1key = stnew.nextToken();
236 String sphere2key = stnew.nextToken();
237 Sphere sphere1 = (Sphere) this.kamiwaai.ENames
238 .get(sphere1key);
239 Sphere sphere2 = (Sphere) this.kamiwaai.ENames
240 .get(sphere2key);
241 MultiVector meet = sphere1.meet(sphere2);
242 double meet2 = meet.ScProd(meet);
243 double tolerance = 0.00001;
244 if (meet2 < -tolerance) {
245 System.out
246 .println("No intersection circle left!");
247 this.kamiwaai.ENames.remove(element);
248 this.kamiwaai.vdlist.add(element);
249
250 } else if (-tolerance < meet2 && tolerance > meet2) {
251 System.out
252 .println("One point of intersection (touch) left!");
253 Circle SiStouch = new Circle(meet);
254 SwingDrawable ispoint = new PointC(SiStouch
255 .Center());
256 this.kamiwaai.pnum = this.kamiwaai.pnum + 1;
257 String key = "P"
258 + String.valueOf(this.kamiwaai.pnum);
259 this.kamiwaai.ENames.put(key, ispoint);
260 String Clentry = key + "," + sphere1key + ","
261 + sphere2key;
262 this.kamiwaai.Clist.add(Clentry);
263 this.kamiwaai.ENames.remove(element);
264 this.kamiwaai.vdlist.add(element);
265
266 } else if (meet2 >= tolerance) {
267 System.out
268 .println("Circle of intersection redone!");
269 Circle SiScircle = new Circle(meet);
270 SiScircle.setColor(this.kamiwaai.circlecolor);
271 this.kamiwaai.ENames.put(element, SiScircle);
272 this.kamiwaai.rdlist.add(element);
273 }
274 }
275 }
276 while (element.startsWith("S") && constrNumber == 1
277 && st.hasMoreTokens()) {
278 String nextt = st.nextToken();
279 if (this.kamiwaai.mvdPoint.equals(nextt)
280 || this.kamiwaai.rdlist.contains(nextt)) {
281
282 MultiVector newCtMV = ((PointC) this.kamiwaai.ENames
283 .get(this.kamiwaai.mvdPoint))
284 .getMultiVector();
285 Sphere theSphere = (Sphere) this.kamiwaai.ENames
286 .get(element);
287 theSphere.recenter(newCtMV);
288 this.kamiwaai.ENames.put(element, theSphere);
289
290 this.kamiwaai.rdlist.add(element);
291 }
292 }
293 while (element.startsWith("S") && constrNumber == 4
294 && st.hasMoreTokens()) {
295 String nextt = st.nextToken();
296 if (this.kamiwaai.mvdPoint.equals(nextt)
297 || this.kamiwaai.rdlist.contains(nextt)) {
298 System.out
299 .println("Moving a 4p sphere! Moved Point No. "
300 + this.kamiwaai.mvdPoint);
301
302
303 StringTokenizer stnew = new StringTokenizer(Citem,
304 ",");
305 stnew.nextToken();
306 PointC sp1 = (PointC) this.kamiwaai.ENames
307 .get(stnew.nextToken());
308 PointC sp2 = (PointC) this.kamiwaai.ENames
309 .get(stnew.nextToken());
310 PointC sp3 = (PointC) this.kamiwaai.ENames
311 .get(stnew.nextToken());
312 PointC sp4 = (PointC) this.kamiwaai.ENames
313 .get(stnew.nextToken());
314 Sphere newSphere = new Sphere(sp1, sp2, sp3, sp4);
315 newSphere.setColor(this.kamiwaai.spherecolor);
316 this.kamiwaai.ENames.put(element, newSphere);
317
318 this.kamiwaai.rdlist.add(element);
319 }
320 }
321 while (element.startsWith("P") && constrNumber == 2
322 && elcomp2.startsWith("L") && st.hasMoreTokens()) {
323
324
325
326
327
328
329
330
331
332
333 if (this.kamiwaai.rdlist.contains(st.nextToken())) {
334
335
336 StringTokenizer stnew = new StringTokenizer(Citem,
337 ",");
338 stnew.nextToken();
339 String skey = stnew.nextToken();
340 String lkey = stnew.nextToken();
341 Sphere sphere = (Sphere) this.kamiwaai.ENames
342 .get(skey);
343 Line line = (Line) this.kamiwaai.ENames.get(lkey);
344 Vector meet = sphere.MeetLineMVs(line);
345 if (meet.size() == 0) {
346 System.out
347 .println("Intersection point(s) vanished!");
348 this.kamiwaai.ENames.remove(element);
349 this.kamiwaai.vdlist.add(element);
350
351
352 } else if (meet.size() == 1) {
353 SwingDrawable ispoint = new PointC(
354 (MultiVector) meet.get(0));
355 this.kamiwaai.ENames.put(element, ispoint);
356 this.kamiwaai.rdlist.add(element);
357 } else if (meet.size() == 2) {
358 System.out
359 .println("Now two points of intersection!");
360 this.kamiwaai.ENames.remove(element);
361 this.kamiwaai.rdlist.add(element);
362
363
364 SwingDrawable ispoint1 = new PointC(
365 (MultiVector) meet.get(0));
366 this.kamiwaai.pnum = this.kamiwaai.pnum + 1;
367 String key1 = "P"
368 + String.valueOf(this.kamiwaai.pnum);
369 this.kamiwaai.ENames.put(key1, ispoint1);
370 SwingDrawable ispoint2 = new PointC(
371 (MultiVector) meet.get(1));
372 this.kamiwaai.pnum = this.kamiwaai.pnum + 1;
373 String key2 = "P"
374 + String.valueOf(this.kamiwaai.pnum);
375 this.kamiwaai.ENames.put(key2, ispoint2);
376 String Clentry1 = key1 + ","
377 + this.kamiwaai.selectedSphere + ","
378 + this.kamiwaai.selectedLine + ","
379 + key2;
380 String Clentry2 = key2 + ","
381 + this.kamiwaai.selectedSphere + ","
382 + this.kamiwaai.selectedLine + ","
383 + key1;
384 this.kamiwaai.Clist.add(Clentry1);
385 this.kamiwaai.Clist.add(Clentry2);
386 }
387 }
388 }
389 while (element.startsWith("P") && constrNumber == 2
390 && elcomp2.startsWith("S") && st.hasMoreTokens()) {
391
392
393
394
395
396
397
398
399
400 if (this.kamiwaai.rdlist.contains(st.nextToken())) {
401
402
403 StringTokenizer stnew = new StringTokenizer(Citem,
404 ",");
405 stnew.nextToken();
406 String s1key = stnew.nextToken();
407 String s2key = stnew.nextToken();
408 Sphere sphere1 = (Sphere) this.kamiwaai.ENames
409 .get(s1key);
410 Sphere sphere2 = (Sphere) this.kamiwaai.ENames
411 .get(s2key);
412 MultiVector meet = sphere1.meet(sphere2);
413 double meet2 = meet.ScProd(meet);
414 double tolerance = 0.00001;
415 if (meet2 < -tolerance) {
416 System.out
417 .println("No intersection circle left!");
418 this.kamiwaai.ENames.remove(element);
419 this.kamiwaai.vdlist.add(element);
420
421 } else if (-tolerance < meet2 && tolerance > meet2) {
422 System.out
423 .println("Again one point of intersection (touch)!");
424 Circle SiStouch = new Circle(meet);
425 SwingDrawable ispoint = new PointC(SiStouch
426 .Center());
427 this.kamiwaai.ENames.put(element, ispoint);
428 this.kamiwaai.rdlist.add(element);
429 } else if (meet2 >= tolerance) {
430 System.out
431 .println("Now a full circle of intersection!");
432 Circle SiScircle = new Circle(meet);
433 SiScircle.setColor(this.kamiwaai.circlecolor);
434 this.kamiwaai.cnum = this.kamiwaai.cnum++;
435 String key = "C"
436 + String.valueOf(this.kamiwaai.cnum);
437 String Centry = key + "," + s1key + "," + s2key;
438 this.kamiwaai.ENames.put(key, SiScircle);
439 this.kamiwaai.Clist.add(Centry);
440 this.kamiwaai.ENames.remove(element);
441 this.kamiwaai.vdlist.add(element);
442
443 }
444 }
445 }
446 while (element.startsWith("P") && constrNumber == 3
447 && st.hasMoreTokens()) {
448
449
450
451
452
453
454
455
456
457
458 if (this.kamiwaai.rdlist.contains(st.nextToken())) {
459
460
461
462
463
464 StringTokenizer stnew = new StringTokenizer(Citem,
465 ",");
466 stnew.nextToken();
467 String skey = stnew.nextToken();
468 String lkey = stnew.nextToken();
469 String pairkey = stnew.nextToken();
470 Sphere sphere = (Sphere) this.kamiwaai.ENames
471 .get(skey);
472 Line line = (Line) this.kamiwaai.ENames.get(lkey);
473 Vector meet = sphere.MeetLineMVs(line);
474 if (meet.size() == 0) {
475 System.out
476 .println("Intersection point(s) vanished!");
477 this.kamiwaai.ENames.remove(element);
478 this.kamiwaai.vdlist.add(element);
479 this.kamiwaai.ENames.remove(pairkey);
480 this.kamiwaai.vdlist.add(pairkey);
481
482
483 } else if (meet.size() == 1) {
484
485
486
487
488
489 this.kamiwaai.ENames.remove(element);
490 this.kamiwaai.vdlist.add(element);
491 this.kamiwaai.ENames.remove(pairkey);
492 this.kamiwaai.vdlist.add(pairkey);
493
494
495 SwingDrawable ispoint = new PointC(
496 (MultiVector) meet.get(0));
497 this.kamiwaai.pnum = this.kamiwaai.pnum + 1;
498 String key = "P"
499 + String.valueOf(this.kamiwaai.pnum);
500 this.kamiwaai.ENames.put(key, ispoint);
501 String Clentry = key + ","
502 + this.kamiwaai.selectedSphere + ","
503 + this.kamiwaai.selectedLine;
504 this.kamiwaai.Clist.add(Clentry);
505 } else if (meet.size() == 2
506 && element.compareTo(pairkey) < 0) {
507 System.out
508 .println("Two points of intersection remain!");
509
510
511 SwingDrawable ispoint1 = new PointC(
512 (MultiVector) meet.get(0));
513 this.kamiwaai.ENames.put(element, ispoint1);
514 SwingDrawable ispoint2 = new PointC(
515 (MultiVector) meet.get(1));
516 this.kamiwaai.ENames.put(pairkey, ispoint2);
517 this.kamiwaai.rdlist.add(element);
518 this.kamiwaai.rdlist.add(pairkey);
519 }
520 }
521 }
522 }
523
524
525 for (int i = this.kamiwaai.Clist.size() - 1; i >= 0; i--) {
526
527 String citem = (String) this.kamiwaai.Clist.get(i);
528 StringTokenizer st = new StringTokenizer(citem, ",");
529
530
531
532
533 while (st.hasMoreTokens()) {
534 String element = st.nextToken();
535
536 if (this.kamiwaai.vdlist.contains(element)) {
537 this.kamiwaai.Clist.remove(i);
538
539
540 StringTokenizer stnew = new StringTokenizer(citem,
541 ",");
542 String vditem = stnew.nextToken();
543 this.kamiwaai.vdlist.add(vditem);
544 this.kamiwaai.ENames.remove(vditem);
545 break;
546
547
548 }
549 }
550 }
551
552
553
554
555
556 this.kamiwaai.vdlist.clear();
557
558
559 this.kamiwaai.rdlist.clear();
560 }
561
562
563 Graphics2D g = (Graphics2D) this.kamiwaai.m_malflaeche
564 .getGraphics();
565 g.clearRect(0, 0, 300, 300);
566 Graphics2D gZ = (Graphics2D) this.kamiwaai.m_malflaecheZ
567 .getGraphics();
568 gZ.clearRect(0, 0, 300, 300);
569
570
571 for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
572 StringTokenizer st = new StringTokenizer(
573 (String) this.kamiwaai.Clist.get(i), ",");
574 String element = st.nextToken();
575 SwingDrawable renewObject = (SwingDrawable) this.kamiwaai.ENames
576 .get(element);
577 renewObject.draw(g);
578 renewObject.drawZ(gZ);
579 }
580 }
581 }
582 /***
583 *
584 */
585 void redrawPoints() {
586
587 for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
588 StringTokenizer st = new StringTokenizer(
589 (String) this.kamiwaai.Clist.get(i), ",");
590 String element = st.nextToken();
591 if (element.startsWith("P")) {
592 SwingDrawable renewPt = (SwingDrawable) this.kamiwaai.ENames
593 .get(element);
594 renewPt.draw(this.kamiwaai.m_malflaeche.getGraphics());
595 renewPt
596 .drawZ(this.kamiwaai.m_malflaecheZ
597 .getGraphics());
598 }
599 }
600 }
601 /***
602 * @param Poles
603 * @return
604 */
605 static double getPolesY(MultiVector Poles) {
606 return Poles.ScProd(MultiVectors.e2());
607 }
608 /***
609 * @param Poles
610 * @return
611 */
612 static double getPolesX(MultiVector Poles) {
613 return Poles.ScProd(MultiVectors.e1());
614 }
615 /***
616 * @param Pole
617 * @return
618 */
619 static double getPoleY(MultiVector Pole) {
620 return Pole.ScProd(MultiVectors.e2());
621 }
622 /***
623 * @param Pole
624 * @return
625 */
626 static double getPoleX(MultiVector Pole) {
627 return Pole.ScProd(MultiVectors.e1());
628 }
629 /***
630 * @param pt
631 * @return
632 */
633 static double getYCoordinate(Point pt) {
634 return pt.getY();
635 }
636 /***
637 * @param pt
638 * @return
639 */
640 static double getXCoordinate(Point pt) {
641 return pt.getX();
642 }
643 /***
644 * @param Poles
645 * @return
646 */
647 static double getPolesZ(MultiVector Poles) {
648 return Poles.ScProd(MultiVectors.e3());
649 }
650 /***
651 * @param Pole
652 * @return
653 */
654 static double getPoleZ(MultiVector Pole) {
655 return Pole.ScProd(MultiVectors.e3());
656 }
657 abstract double getDPoles(Point pt, MultiVector Poles);
658 abstract double getDPole(Point pt, MultiVector Pole);
659 /***
660 * Return key of the sphere that is chosen, if any, or " " otherwise.
661 *
662 */
663 private String pickSphere(Point pt) {
664 double dp;
665 double delta = 5.0;
666 String spherekey = " ";
667 for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
668 StringTokenizer st = new StringTokenizer(
669 (String) this.kamiwaai.Clist.get(i), ",");
670 String element = st.nextToken();
671 if (element.startsWith("S")) {
672 Sphere onesphere = (Sphere) this.kamiwaai.ENames.get(element);
673 MultiVector Pole = onesphere.PonS();
674 MultiVector Poles = onesphere.PonSsouth();
675 if (getDPole(pt, Pole) <= getDPoles(pt, Poles)) {
676 dp = getDPole(pt, Pole);
677 } else
678 dp = getDPoles(pt, Poles);
679 if (dp <= delta) {
680 spherekey = element;
681 delta = dp;
682 }
683 }
684 }
685 return spherekey;
686 }
687 /***
688 * @param e
689 */
690 void sphereIntersectSphere(MouseEvent e) {
691
692 if (k == 0) {
693 System.out.println("Selecting the 1st sphere!");
694 Point pts1 = e.getPoint();
695 this.kamiwaai.selectedSphere1 = pickSphere(pts1);
696 System.out.println("1st sphere "
697 + this.kamiwaai.selectedSphere1 + " selected.");
698 if (this.kamiwaai.selectedSphere1 != " ") {
699 k = 1;
700 System.out.println("And now select the 2nd sphere!");
701 } else {
702 System.out.println("Try again to select the 1st sphere!");
703 }
704 } else if (k == 1) {
705
706 Point pts2 = e.getPoint();
707 this.kamiwaai.selectedSphere2 = pickSphere(pts2);
708 if (this.kamiwaai.selectedSphere2.equals(" ")
709 || this.kamiwaai.selectedSphere1
710 .equals(this.kamiwaai.selectedSphere2)) {
711 System.out.println("Try again to select the 2nd sphere!");
712 }
713
714
715
716 else {
717 System.out.println("Sphere "
718 + this.kamiwaai.selectedSphere2 + " selected.");
719 Sphere sphere1 = (Sphere) this.kamiwaai.ENames
720 .get(this.kamiwaai.selectedSphere1);
721 Sphere sphere2 = (Sphere) this.kamiwaai.ENames
722 .get(this.kamiwaai.selectedSphere2);
723
724 MultiVector meet = sphere1.meet(sphere2);
725 double meet2 = meet.ScProd(meet);
726 double tolerance = 0.00001;
727 if (meet2 < 0)
728 System.out.println("No intersection!" + "\n"
729 + "Select new spheres!");
730 else if (-tolerance < meet2 && tolerance > meet2) {
731 System.out.println("One point of intersection (touch)!"
732 + "\n" + "Select a new line!");
733 Circle SiStouch = new Circle(meet);
734 SwingDrawable ispoint = new PointC(SiStouch.Center());
735 this.kamiwaai.pnum = this.kamiwaai.pnum + 1;
736 String key = "P" + String.valueOf(this.kamiwaai.pnum);
737 this.kamiwaai.ENames.put(key, ispoint);
738 String Clentry = key + ","
739 + this.kamiwaai.selectedSphere1 + ","
740 + this.kamiwaai.selectedSphere2;
741 this.kamiwaai.Clist.add(Clentry);
742 ispoint.draw(this.kamiwaai.m_malflaeche.getGraphics());
743 ispoint
744 .drawZ(this.kamiwaai.m_malflaecheZ
745 .getGraphics());
746 } else if (meet2 >= tolerance) {
747 System.out.println("Circle of intersection!" + "\n"
748 + "Select new spheres!");
749 Circle SiScircle = new Circle(meet);
750 SiScircle.setColor(this.kamiwaai.circlecolor);
751 this.kamiwaai.cnum = this.kamiwaai.cnum + 1;
752 String key = "C" + String.valueOf(this.kamiwaai.cnum);
753 this.kamiwaai.ENames.put(key, SiScircle);
754 SiScircle
755 .draw(this.kamiwaai.m_malflaeche.getGraphics());
756 SiScircle.drawZ(this.kamiwaai.m_malflaecheZ
757 .getGraphics());
758 String Clentry = key + ","
759 + this.kamiwaai.selectedSphere1 + ","
760 + this.kamiwaai.selectedSphere2;
761 this.kamiwaai.Clist.add(Clentry);
762 }
763 k = 0;
764 }
765 }
766 }
767
768 abstract String pickLine(Point pt);
769 abstract String pickPoint(Point pt);
770
771 /***
772 * @param e
773 */
774 void sphereIntersectLine(MouseEvent e) {
775 if (k == 0) {
776 System.out.println("Selecting a line!");
777 Point ptl = e.getPoint();
778 this.kamiwaai.selectedLine = pickLine(ptl);
779 System.out.println("Line " + this.kamiwaai.selectedLine
780 + " selected.");
781 if (this.kamiwaai.selectedLine != " ") {
782 k = 1;
783 System.out.println("And now select a sphere!");
784 } else {
785 System.out.println("Try again to select a line!");
786 }
787 } else if (k == 1) {
788
789 Point pts = e.getPoint();
790 this.kamiwaai.selectedSphere = pickSphere(pts);
791
792 if (this.kamiwaai.selectedSphere.equals(" ")) {
793 System.out.println("Try again to select a sphere!");
794 }
795
796
797
798 else {
799 System.out.println("Sphere " + this.kamiwaai.selectedSphere
800 + " selected.");
801 Line line = (Line) this.kamiwaai.ENames
802 .get(this.kamiwaai.selectedLine);
803 Sphere sphere = (Sphere) this.kamiwaai.ENames
804 .get(this.kamiwaai.selectedSphere);
805 Vector meet = sphere.MeetLineMVs(line);
806 if (meet.size() == 0)
807 System.out.println("No intersection!" + "\n"
808 + "Select a new line!");
809 else if (meet.size() == 1) {
810 System.out.println("One point of intersection!" + "\n"
811 + "Select a new line!");
812 SwingDrawable ispoint = new PointC((MultiVector) meet
813 .get(0));
814 this.kamiwaai.pnum = this.kamiwaai.pnum + 1;
815 String key = "P" + String.valueOf(this.kamiwaai.pnum);
816 this.kamiwaai.ENames.put(key, ispoint);
817 String Clentry = key + ","
818 + this.kamiwaai.selectedSphere + ","
819 + this.kamiwaai.selectedLine;
820 this.kamiwaai.Clist.add(Clentry);
821 ispoint.draw(this.kamiwaai.m_malflaeche.getGraphics());
822 ispoint
823 .drawZ(this.kamiwaai.m_malflaecheZ
824 .getGraphics());
825 } else if (meet.size() == 2) {
826 System.out.println("Two points of intersection!" + "\n"
827 + "Select a new line!");
828 SwingDrawable ispoint1 = new PointC((MultiVector) meet
829 .get(0));
830 this.kamiwaai.pnum = this.kamiwaai.pnum + 1;
831 String key1 = "P" + String.valueOf(this.kamiwaai.pnum);
832 this.kamiwaai.ENames.put(key1, ispoint1);
833 ispoint1.draw(this.kamiwaai.m_malflaeche.getGraphics());
834 ispoint1.drawZ(this.kamiwaai.m_malflaecheZ
835 .getGraphics());
836 SwingDrawable ispoint2 = new PointC((MultiVector) meet
837 .get(1));
838 this.kamiwaai.pnum = this.kamiwaai.pnum + 1;
839 String key2 = "P" + String.valueOf(this.kamiwaai.pnum);
840 this.kamiwaai.ENames.put(key2, ispoint2);
841 String Clentry1 = key1 + ","
842 + this.kamiwaai.selectedSphere + ","
843 + this.kamiwaai.selectedLine + "," + key2;
844 String Clentry2 = key2 + ","
845 + this.kamiwaai.selectedSphere + ","
846 + this.kamiwaai.selectedLine + "," + key1;
847 this.kamiwaai.Clist.add(Clentry1);
848 this.kamiwaai.Clist.add(Clentry2);
849 ispoint2.draw(this.kamiwaai.m_malflaeche.getGraphics());
850 ispoint2.drawZ(this.kamiwaai.m_malflaecheZ
851 .getGraphics());
852 }
853 k = 0;
854 }
855 }
856 }
857
858 /***
859 * @param e
860 */
861 void circleMode(MouseEvent e) {
862 Point pt = e.getPoint();
863 String pickPt = pickPoint(pt);
864 if (pickPt.startsWith("P") && k < 3) {
865 this.kamiwaai.CirclePoints[k] = pickPt;
866 System.out.println("point " + this.kamiwaai.CirclePoints[k]
867 + " chosen!");
868 k = k + 1;
869 }
870
871 if (k == 3
872 && (this.kamiwaai.CirclePoints[0]
873 .equals(this.kamiwaai.CirclePoints[1])
874 || this.kamiwaai.CirclePoints[1]
875 .equals(this.kamiwaai.CirclePoints[2]) || this.kamiwaai.CirclePoints[2]
876 .equals(this.kamiwaai.CirclePoints[0]))) {
877 System.out.println("Choose again 3 different points!");
878 k = 0;
879 } else if (k == 3) {
880 drawCircle();
881 }
882 }
883 /***
884 * @param e
885 */
886 void sphereMode(MouseEvent e) {
887 Point pt = e.getPoint();
888 String pickPt = pickPoint(pt);
889 if (pickPt.startsWith("P") && k < 4) {
890 this.kamiwaai.SpherePoints[k] = pickPt;
891 System.out.println("point " + this.kamiwaai.SpherePoints[k]
892 + " chosen!");
893 k = k + 1;
894 }
895
896 if (k == 4
897 && (this.kamiwaai.SpherePoints[0]
898 .equals(this.kamiwaai.SpherePoints[1])
899 || this.kamiwaai.SpherePoints[1]
900 .equals(this.kamiwaai.SpherePoints[2])
901 || this.kamiwaai.SpherePoints[2]
902 .equals(this.kamiwaai.SpherePoints[3]) || this.kamiwaai.SpherePoints[3]
903 .equals(this.kamiwaai.SpherePoints[0]))) {
904 System.out.println("Choose again 4 different points!");
905 k = 0;
906 } else if (k == 4) {
907 drawSphere();
908 }
909 }
910 /***
911 * @param e
912 */
913 void lineMode(MouseEvent e) {
914 Point pt = e.getPoint();
915 String pickPt = pickPoint(pt);
916 if (pickPt.startsWith("P") && k < 2) {
917 this.kamiwaai.LinePoints[k] = pickPt;
918 System.out.println("point " + this.kamiwaai.LinePoints[k]
919 + " chosen!");
920 k = k + 1;
921 }
922
923 if (k == 2
924 && this.kamiwaai.LinePoints[0]
925 .equals(this.kamiwaai.LinePoints[1])) {
926 System.out.println("Choose again 2 different points!");
927 k = 0;
928 } else if (k == 2) {
929 PointC lp1 = (PointC) this.kamiwaai.ENames
930 .get(this.kamiwaai.LinePoints[0]);
931 PointC lp2 = (PointC) this.kamiwaai.ENames
932 .get(this.kamiwaai.LinePoints[1]);
933 Line L = new Line(lp1, lp2);
934 L.setColor(this.kamiwaai.linecolor);
935 this.kamiwaai.lnum = this.kamiwaai.lnum + 1;
936 String key = "L" + String.valueOf(this.kamiwaai.lnum);
937 this.kamiwaai.ENames.put(key, L);
938 String Clentry = key + "," + this.kamiwaai.LinePoints[0] + ","
939 + this.kamiwaai.LinePoints[1];
940 this.kamiwaai.Clist.add(Clentry);
941 L.draw(this.kamiwaai.m_malflaeche.getGraphics());
942 L.drawZ(this.kamiwaai.m_malflaecheZ.getGraphics());
943 k = 0;
944 }
945 }
946 /***
947 * @param e
948 */
949 void sphereRadiusMode(MouseEvent e) {
950 Point pt = e.getPoint();
951 String pickPt = pickPoint(pt);
952 if (pickPt.startsWith("P")) {
953 drawSphereWithCentre(pickPt);
954 } else
955 System.out.println("Pick a new sphere center!");
956 }
957
958
959
960
961
962
963
964
965
966
967 }