Für die Erstellung der SVG Grafik wurden die Polygone verwendet, welche aus 3 oder 4 Punkten bestehen.
So bestand das Dreieck aus der Basis Länge 2 und der Höhe sqrt(3).
Die Punkte für die Polygon definition waren damit [0,0] [2,0] [1,sqrt(3)].
Da SVG den Nullpunkt links oben hat und die Y-Werte nach untern abträgt wurde die SVG Werte
- mit einem Zoom Faktor vergrößert
- mit einem XY Offset verschoben
- die Y Werte invertiert
Nun sind aber nicht alle Punkte so einfach zu bestimmen und teilweise benötigt man den Schnittpunkt von 2 Geraden.
Hierfür ist ein eigene Funktion entstanden.
Gegeben sind 4 Punkte als Vektor P1,P2,P3,P4 wobei gilt:
G = P1 + a * R1 (1) mit R1 = P2 - P1
F = P3 + b * R3 (2) mit R3 = P4 - P3
Setzt man die beiden Geraden gleich erhält man einen Schnittpunkt sofern der Richtungsvektor r1 nicht parallel zu r3 ist.
Man löst also nach Gleichsetzen nach a auf
P1 + a * R1 = P3 + b * R3 (3)
b * R3 + P3-P1
a = -------------- (4)
R1
und setzt man den a.x Wert in die Gleichung (3) mit y ein. Damit erhält man ein b.
P1.y + a(x)* R1.y = P3.y + b * R3.y
R3.x * R1.y P3.x - P1.x
0 = P1.y - P3.y + b ----------- + ----------- * R1.y - b * R3.y
R1.x R1.x
R1.y
---- * (P1.x - P3.x) - ( P1.y - P3.y)
R1.x
b = -----------------------------------------
R1.y
---- * R3.x - R3.y
R1.x
Damit ist b bestimmt und damit der Schnittpunkt, wenn b in (2) eingetzt wird.
Allerdings nur, wenn R1.x ungleich Null ist.
Dann geht es damit, dass aus Gleichung (4) nicht a.x, sondern a.y bestimmt wird.
R1.x
---- * (P1.y - P3.y) - ( P1.x - P3.x)
R1.y
b = -----------------------------------------
R1.x
---- * R3.y - R3.x
R1.y
Source Code:
Dreieck.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dreieck;
import static java.lang.Math.sqrt;
/**
*
* @author LangR
*/
public class Dreieck {
static int zoom = 200;
public static Point cross(Point p1, Point p2, Point p3, Point p4) {
/*
Schnittpunkt von 2 Geraden, p1,p2 bilden g(x) p3,p4 bilden f(x)
g(x) = p1 + a * (p2-p1)
f(x) = p3 + b * (p4-p3)
*/
Point r1 = new Point(p2.x - p1.x, p2.y - p1.y);
Point r3 = new Point(p4.x - p3.x, p4.y - p3.y);
Point d = new Point(p1.x - p3.x, p1.y - p3.y);
double b;
if (r1.x != 0.0) // avoid div by ZERO
{
b = (r1.y / r1.x * d.x - d.y) / (r1.y / r1.x * r3.x - r3.y);
} else {
b = (r1.x / r1.y * d.y - d.x) / (r1.x / r1.y * r3.y - r3.x);
}
return new Point(p3.x + b * r3.x, p3.y + b * r3.y);
}
public static String poly(String col, Point p1, Point p2, Point p3, Point p4) {
StringBuffer buf = new StringBuffer();
buf.append(" <polygon points=\"");
buf.append(p1.get(zoom));
buf.append(",");
buf.append(p2.get(zoom));
buf.append(",");
buf.append(p3.get(zoom));
if (p4 != null) {
buf.append(",");
buf.append(p4.get(zoom));
}
//buf.append("\" style=\"fill:" + col + ";opacity:0.3; stroke:black;stroke-width:1\"/>\n");
buf.append("\" style=\"fill:" + col + ";fill-opacity:0.3; stroke:black;stroke-width:1\"/>\n");
return buf.toString();
}
public static String pos(Point p,int txt) {
return "<text x=\""+p.getX(zoom)+"\" y=\""+p.getY(zoom)+"\">"+txt+"</text>\n";
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Point p1 = new Point(0, 0);
Point p2 = new Point(1, 0);
Point p3 = new Point(2, 0);
Point p4 = new Point(1, sqrt(3));
Point p5 = new Point(0, sqrt(3));
Point p6 = new Point(1, sqrt(3) - sqrt(sqrt(3) - 1));
Point p7 = new Point(cross(p5, p6, p1, p4));
Point p8 = new Point(0, sqrt(sqrt(3) - 1));
Point p10 = new Point(-sqrt(sqrt(3) - 1), sqrt(3) - 1);
Point p9 = new Point(-1, sqrt(sqrt(3) - 1));
p9 = new Point(cross(p8, p9, p5, p10));
Point p12 = new Point(1 - sqrt(sqrt(3) - 1), sqrt(3) - sqrt(sqrt(3) - 1) - 1);
Point p13 = new Point(cross(p12, p6, p1, p3));
Point p14 = new Point(1 + p7.x, sqrt(3) - p7.y);
Point p15 = new Point(-p6.x + p7.x, p7.y - p6.y);
Point p16 = new Point(1 + p13.x, 0);
Point p17 = new Point(1 + p12.x, -p12.y);
Point p18 = new Point(1, -sqrt(sqrt(3) - 1));
StringBuffer buf = new StringBuffer();
buf.append("<html>");
buf.append("<body>\n"
+ "\n"
+ "<h1>Scherung</h1>");
buf.append("<svg width=\"" + p3.getX(zoom) + "\" height=\"" + p12.getY(zoom) + "\">\n");
buf.append(poly("none", p1, p3, p4, null));
buf.append(poly("blue", p4, p7, p6, null));
buf.append(poly("red", p5, p8, p9, null));
buf.append(poly("green", p1, p5, p7, null));
buf.append(poly("yellow", p15, p8, p9, p10));
buf.append(poly("orange", p1, p12, p13, null));
buf.append(poly("red", p13, p2, p6, null));
buf.append(poly("green", p2, p14, p4, null));
buf.append(poly("blue", p1, p15, p8, null));
buf.append(poly("orange", p2, p16, p17, null));
buf.append(poly("yellow", p3, p16, p17, p14));
buf.append("</svg>\n");
buf.append("<br>\n");
buf.append("<br>\n");
zoom = 50;
buf.append("<svg width=\"" + p3.getX(zoom) + "\" height=\"" + p18.getY(zoom) + "\">\n");
buf.append(poly("none", p1, p12, p13, null));
buf.append(poly("none", p1, p3, p4, null));
buf.append(poly("none", p5, p6, p12, p10));
buf.append(poly("yellow", p1, p2, p4, null));
buf.append(poly("red", p2, p3, p4, null));
buf.append("</svg>\n");
buf.append("<svg width=\"" + p3.getX(zoom) + "\" height=\"" + p18.getY(zoom) + "\">\n");
buf.append(poly("none", p1, p12, p13, null));
buf.append(poly("none", p1, p3, p4, null));
buf.append(poly("none", p5, p6, p12, p10));
buf.append(poly("red", p4, p5, p6, null));
buf.append(poly("yellow", p1, p2, p6, p5));
buf.append("</svg>\n");
buf.append("<svg width=\"" + p3.getX(zoom) + "\" height=\"" + p18.getY(zoom) + "\">\n");
buf.append(poly("none", p1, p12, p13, null));
buf.append(poly("none", p1, p3, p4, null));
buf.append(poly("none", p5, p6, p12, p10));
buf.append(poly("yellow", p1, p12, p6, p5));
buf.append(poly("red", p12, p18, p6, null));
buf.append("</svg>\n");
buf.append("<svg width=\"" + p3.getX(zoom) + "\" height=\"" + p18.getY(zoom) + "\">\n");
buf.append(poly("none", p1, p12, p13, null));
buf.append(poly("none", p1, p3, p4, null));
buf.append(poly("none", p5, p6, p12, p10));
buf.append(poly("yellow", p1, p12, p6, p5));
buf.append(poly("red", p1, p5, p10, null));
buf.append("</svg>\n");
buf.append("<br>Legende<br>");
zoom=200;
buf.append("<svg width=\"" + (p3.getIntX(zoom)+10) + "\" height=\"" + (p18.getIntY(zoom)+10) + "\">\n");
buf.append(poly("none", p1, p3, p4, null));
buf.append(poly("none", p4, p7, p6, null));
buf.append(poly("none", p5, p8, p9, null));
buf.append(poly("none", p1, p5, p7, null));
buf.append(poly("none", p15, p8, p9, p10));
buf.append(poly("none", p1, p12, p13, null));
buf.append(poly("none", p13, p2, p6, null));
buf.append(poly("none", p2, p14, p4, null));
buf.append(poly("none", p1, p15, p8, null));
buf.append(poly("none", p2, p16, p17, null));
buf.append(poly("none", p3, p16, p17, p14));
buf.append(poly("none", p12, p18, p6, null));
buf.append(pos(p1,1));
buf.append(pos(p2,2));
buf.append(pos(p3,3));
buf.append(pos(p4,4));
buf.append(pos(p5,5));
buf.append(pos(p6,6));
buf.append(pos(p7,7));
buf.append(pos(p8,8));
buf.append(pos(p9,9));
buf.append(pos(p10,10));
buf.append(pos(p12,12));
buf.append(pos(p13,13));
buf.append(pos(p14,14));
buf.append(pos(p15,15));
buf.append(pos(p16,16));
buf.append(pos(p17,17));
buf.append(pos(p18,18));
buf.append("</svg>\n");
buf.append("</html>");
System.out.println(buf);
}
}
Point.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dreieck;
/**
*
* @author LangR
*/
public class Point {
Double x;
Double y;
public Point(double x, double y) {
this.x=x;
this.y=y;
}
public Point(Point p) {
this.x=p.x;
this.y=p.y;
}
public void print() {
System.out.println(x+"/"+y);
}
public String get(int zoom) {
return getX(zoom)+","+getY(zoom)+" ";
}
public String getX(int zoom) {
Double d=0.9 * zoom;
int Xofs=d.intValue();
return (Xofs+(x*zoom)+"");
}
public int getIntX(int zoom) {
Double d=0.9 * zoom;
int Xofs=d.intValue();
d=Xofs+(x*zoom);
return d.intValue();
}
public String getY(int zoom) {
Double d=1.8 * zoom;
int Yofs=d.intValue();
return (Yofs-y*zoom+"");
}
public int getIntY(int zoom) {
Double d=1.8 * zoom;
int Yofs=d.intValue();
d=Yofs-y*zoom;
return d.intValue();
}
}