/** * Computes the cross product k = u X v. * * @param v a vertex * @return returns the magnitude of u X v */ double crossProduct(Vertex v) { return (p.x * v.getY() - p.y * v.getX()); }
public boolean equals(Vertex _x) { if (p.x == _x.getX() && p.y == _x.getY()) { return true; } else { return false; } }
/** * Computes the inner or dot product * * @param v a vertex * @return returns the dot product u.v */ double dot(Vertex v) { return (p.x * v.getX() + p.y * v.getY()); }
private HCoordinate bisector(Vertex a, Vertex b) { // returns the perpendicular bisector of the line segment ab double dx = b.getX() - a.getX(); double dy = b.getY() - a.getY(); HCoordinate l1 = new HCoordinate(a.getX() + dx / 2.0, a.getY() + dy / 2.0, 1.0); HCoordinate l2 = new HCoordinate(a.getX() - dy + dx / 2.0, a.getY() + dx + dy / 2.0, 1.0); return new HCoordinate(l1, l2); }
Vertex sum(Vertex v) { return (new Vertex(p.x + v.getX(), p.y + v.getY())); }
Vertex sub(Vertex v) { return (new Vertex(p.x - v.getX(), p.y - v.getY())); }
/** * For this vertex enclosed in a triangle defined by three vertices v0, v1 and v2, interpolate * a z value from the surrounding vertices. */ public double interpolateZValue(Vertex v0, Vertex v1, Vertex v2) { double x0 = v0.getX(); double y0 = v0.getY(); double a = v1.getX() - x0; double b = v2.getX() - x0; double c = v1.getY() - y0; double d = v2.getY() - y0; double det = a * d - b * c; double dx = this.getX() - x0; double dy = this.getY() - y0; double t = (d * dx - b * dy) / det; double u = (-c * dx + a * dy) / det; double z = v0.getZ() + t * (v1.getZ() - v0.getZ()) + u * (v2.getZ() - v0.getZ()); return z; }
/** * returns a new vertex that is mid-way between this vertex and another end point. * * @param a the other end point. * @return the point mid-way between this and that. */ public Vertex midPoint(Vertex a) { double xm = (p.x + a.getX()) / 2.0; double ym = (p.y + a.getY()) / 2.0; double zm = (p.getZ() + a.getZ()) / 2.0; return new Vertex(xm, ym, zm); }
public int classify(Vertex p0, Vertex p1) { Vertex p2 = this; Vertex a = p1.sub(p0); Vertex b = p2.sub(p0); double sa = a.crossProduct(b); if (sa > 0.0) return LEFT; if (sa < 0.0) return RIGHT; if ((a.getX() * b.getX() < 0.0) || (a.getY() * b.getY() < 0.0)) return BEHIND; if (a.magn() < b.magn()) return BEYOND; if (p0.equals(p2)) return ORIGIN; if (p1.equals(p2)) return DESTINATION; return BETWEEN; }
/** * Computes the centre of the circumcircle of this vertex and two others. * * @param b * @param c * @return the Coordinate which is the circumcircle of the 3 points. */ public Vertex circleCenter(Vertex b, Vertex c) { Vertex a = new Vertex(this.getX(), this.getY()); // compute the perpendicular bisector of cord ab HCoordinate cab = bisector(a, b); // compute the perpendicular bisector of cord bc HCoordinate cbc = bisector(b, c); // compute the intersection of the bisectors (circle radii) HCoordinate hcc = new HCoordinate(cab, cbc); Vertex cc = null; try { cc = new Vertex(hcc.getX(), hcc.getY()); } catch (NotRepresentableException nre) { System.err.println("a: " + a + " b: " + b + " c: " + c); System.err.println(nre); } return cc; }