|
| 1 | +package hk.microos.data; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +import hk.microos.tools.UniversalTool; |
| 6 | + |
| 7 | +public class LinearLine { |
| 8 | + private boolean vertical = false; |
| 9 | + private boolean horizontal = false; |
| 10 | + public double k, b; |
| 11 | + |
| 12 | + public LinearLine(double k, double b) { |
| 13 | + this.k = k; |
| 14 | + this.b = b; |
| 15 | + } |
| 16 | + |
| 17 | + public LinearLine(double k, double b, boolean vertical, boolean horizontal) { |
| 18 | + this.k = k; |
| 19 | + this.b = b; |
| 20 | + this.vertical = vertical; |
| 21 | + this.horizontal = horizontal; |
| 22 | + } |
| 23 | + |
| 24 | + public LinearLine(Point_ a, Point_ b) { |
| 25 | + if (Math.abs(a.y-b.y) < 0.0000001) { |
| 26 | + horizontal = true; |
| 27 | + }else if (Math.abs(a.x-b.x) < 0.0000001) { |
| 28 | + vertical = true; |
| 29 | + } else { |
| 30 | + this.k = (a.y - b.y) / (a.x - b.x); |
| 31 | + this.b = a.y - this.k * a.x; |
| 32 | + } |
| 33 | + } |
| 34 | + public double calY(double X){ |
| 35 | + return this.k*X+b; |
| 36 | + } |
| 37 | + public double calX(double Y){ |
| 38 | + return (Y-this.b)/this.k; |
| 39 | + } |
| 40 | + public Point_ projectOnLine(double x0, double y0, int minX, int minY, int maxX, int maxY){ |
| 41 | + if(this.vertical){ |
| 42 | + return new Point_(this.b, y0); |
| 43 | + } |
| 44 | + if(this.horizontal){ |
| 45 | + return new Point_(x0, this.b); |
| 46 | + } |
| 47 | + Point_ p = Math.abs(this.k) < 1?new Point_(x0, this.calY(x0)): new Point_(this.calX(y0),y0); |
| 48 | + if(UniversalTool.inBound(p, minX, minY, maxX, maxY)){ |
| 49 | + return p; |
| 50 | + }else{ |
| 51 | + double imgTan = (maxY-minY)/(maxX-minX); |
| 52 | + Point_ intersect1, intersect2; |
| 53 | + if(Math.abs(this.k) >= imgTan){ |
| 54 | + intersect1 = new Point_(this.calX(minY),minY); |
| 55 | + intersect2 = new Point_(this.calX(maxY),maxY); |
| 56 | + }else{ |
| 57 | + intersect1 = new Point_(minX, calY(minX)); |
| 58 | + intersect2 = new Point_(maxX, calY(maxX)); |
| 59 | + } |
| 60 | + return (UniversalTool.distance(p,intersect1) < UniversalTool.distance(p,intersect2))?intersect1:intersect2; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + } |
| 65 | + public Point_[] getPerpendicularLineEndPoints(double x0, double y0, double halfLong) { |
| 66 | + if(halfLong < 0.5) { |
| 67 | + System.out.println("HL "+halfLong); |
| 68 | + halfLong = 5; |
| 69 | + } |
| 70 | + Point_[] ends = new Point_[2]; |
| 71 | + if(this.horizontal){ |
| 72 | + ends[0] = new Point_(x0, y0+halfLong); |
| 73 | + ends[1] = new Point_(x0, y0-halfLong); |
| 74 | + return ends; |
| 75 | + } |
| 76 | + if(this.vertical){ |
| 77 | + ends[0] = new Point_(x0-halfLong, y0); |
| 78 | + ends[1] = new Point_(x0+halfLong, y0); |
| 79 | + return ends; |
| 80 | + } |
| 81 | + LinearLine pll = this.getPependicularLinearLineAt(x0, y0); |
| 82 | + double offsetX = Math.sqrt((halfLong*halfLong)/(1+pll.k*pll.k)); |
| 83 | + double x1 = x0+offsetX; |
| 84 | + double y1 = pll.calY(x1); |
| 85 | + double x2 = x0-offsetX; |
| 86 | + double y2 = pll.calY(x2); |
| 87 | + ends[0] = new Point_(x1, y1); |
| 88 | + ends[1] = new Point_(x2, y2); |
| 89 | + return ends; |
| 90 | + } |
| 91 | + |
| 92 | + public LinearLine getPependicularLinearLineAt(double x0, double y0) { |
| 93 | + // if this line is vertical |
| 94 | + if (this.vertical) { |
| 95 | + //return a horizontal line |
| 96 | + return new LinearLine(0, y0, false, true); |
| 97 | + } |
| 98 | + //if this line is horizontal |
| 99 | + if (this.horizontal) { |
| 100 | + //return a vertical line |
| 101 | + return new LinearLine(0, x0, true, false); |
| 102 | + } |
| 103 | + double k_ = -1 / this.k; |
| 104 | + double b_ = y0 - k_ * x0; |
| 105 | + return new LinearLine(k_, b_); |
| 106 | + } |
| 107 | + |
| 108 | + public String toString() { |
| 109 | + String s = "LinearLine: "; |
| 110 | + s += " k=" + this.k; |
| 111 | + s += " b=" + this.b; |
| 112 | + s += " V=" + this.vertical; |
| 113 | + s += " H=" + this.horizontal; |
| 114 | + return s; |
| 115 | + } |
| 116 | +} |
0 commit comments