/** * Check if a point is contained within this polygon * * @param point * @return true if contained, false otherwise */ public boolean contains(Vector2D point) { if (!getBounds().contains(point)) { System.out.println("AAA: " + point); return false; } Line2D verticalRay = new Line2D(point, new Vector2D(point.getX(), Double.POSITIVE_INFINITY)); int intersectionCount = 0; boolean contains = false; generateEdges(); for (Enumeration e = edges.elements(); e.hasMoreElements(); ) { Line2D testLine = (Line2D) e.nextElement(); double minY = Math.min(testLine.getStart().getY(), testLine.getEnd().getY()); double maxY = Math.max(testLine.getStart().getY(), testLine.getEnd().getY()); if (testLine.getGradient() == Double.POSITIVE_INFINITY) { if (point.getX() == testLine.getStart().getX() && point.getY() >= minY && point.getY() <= maxY) { contains = true; } } else { if (verticalRay.intersects(testLine)) { intersectionCount++; } } } return (contains || (intersectionCount % 2 == 1)); }
/** * Get the bounding rectangle * * @return minimum bounding rectangle */ public BoundingRectangle2D getBounds() { if (boundsChanged == true) { boolean first = true; double minX = 0; double maxX = 0; double minY = 0; double maxY = 0; for (Enumeration e = vertices.elements(); e.hasMoreElements(); ) { Vector2D vertex = (Vector2D) e.nextElement(); if (first) { minX = vertex.getX(); maxX = vertex.getX(); minY = vertex.getY(); maxY = vertex.getY(); first = false; } else { minX = Math.min(minX, vertex.getX()); maxX = Math.max(maxX, vertex.getX()); minY = Math.min(minY, vertex.getY()); maxY = Math.max(maxY, vertex.getY()); } } bounds.set(minX, minY, Math.abs(maxX - minX), Math.abs(maxY - minY)); boundsChanged = false; } return bounds; }
private static Shape makeLine(Vector2D base, Vector2D disp) { double x1 = base.getX(); double y1 = base.getY(); double x2 = x1 + disp.getX(); double y2 = y1 + disp.getY(); return new Line2D.Double(x1, y1, x2, y2); }
public Frame(Point2D point, Vector2D vec1, Vector2D vec2) throws NoninvertibleTransformException { toStd_ = new AffineTransform( vec1.getX(), vec1.getY(), vec2.getX(), vec2.getY(), point.getX(), point.getY()); fromStd_ = toStd_.createInverse(); origin_ = (Point2D) point.clone(); xAxis_ = new Vector2D(vec1); yAxis_ = new Vector2D(vec2); }
/** * Construct a matrix from its column vectors. * * @param column0 Column vector for column 0 * @param column1 Column vector for column 1 * @param column2 Column vector for column 2 * @param column3 Column vector for column 3 * @param column4 Column vector for column 4 */ public Matrix2x5( Vector2D column0, Vector2D column1, Vector2D column2, Vector2D column3, Vector2D column4) { super(); this.r0c0 = column0.getX(); this.r1c0 = column0.getY(); this.r0c1 = column1.getX(); this.r1c1 = column1.getY(); this.r0c2 = column2.getX(); this.r1c2 = column2.getY(); this.r0c3 = column3.getX(); this.r1c3 = column3.getY(); this.r0c4 = column4.getX(); this.r1c4 = column4.getY(); }
/** * Rotate the polygon clockwise about a point by an arbritray angle. * * @param x X ordinate of point to rotate about * @param y Y ordinate of point to rotate about * @param angle Angle in radians */ public void rotate(double x, double y, double angle) { double theta = -angle; for (Enumeration e = vertices(); e.hasMoreElements(); ) { Vector2D vertex = (Vector2D) e.nextElement(); // translate to origin double tmpX = vertex.getX() - x; double tmpY = vertex.getY() - y; // rotate double sin = Math.sin(theta); double cos = Math.cos(theta); double newX = tmpX * cos - tmpY * sin; double newY = tmpX * sin + tmpY * cos; // translate back to old location newX += x; newY += y; // set teh point to be where we calculated it should be vertex.setXY(newX, newY); } flagModified(); }
/** Generate the edges of this polygon */ private void generateEdges() { if (haveEdgesChanged == true) { edges.removeAllElements(); Vector2D first = null; Vector2D lastChecked = null; double x = 0; double y = 0; for (Enumeration e = vertices.elements(); e.hasMoreElements(); ) { Vector2D vertex = (Vector2D) e.nextElement(); x += vertex.getX(); y += vertex.getY(); if (lastChecked == null) { first = vertex; lastChecked = first; } else { edges.addElement(new Line2D(lastChecked, vertex)); lastChecked = vertex; } } if (first != null) { edges.addElement(new Line2D(lastChecked, first)); } } haveEdgesChanged = false; }
/** * Get the midpoint of this polygon * * @return Midpoint */ public Vector2D midPoint() { if (isMidPointChanged == true) { double x = 0; double y = 0; for (Enumeration e = vertices.elements(); e.hasMoreElements(); ) { Vector2D vertex = (Vector2D) e.nextElement(); x += vertex.getX(); y += vertex.getY(); } midPoint = new Vector2D(x / vertices.size(), y / vertices.size()); isMidPointChanged = false; return midPoint; } else { return midPoint; } }
public static Vector2D multiplicacionPorConstante(Vector2D a, double c) { return new Vector2D(a.getX() * c, a.getY() * c); }
public static double productoPunto(Vector2D a, Vector2D b) { return a.getX() * b.getX() + a.getY() * b.getY(); }
@Test public void constuctorShouldSaveFirstParamInAttributeX() { Vector2D v = new Vector2D(5, 7); Assert.assertEquals(5, v.getX(), EPS); }
public static GridPoint toGridPoint(Vector2D vector, float scaleX, float scaleY) { int x = (int) (vector.getX() * scaleX); int y = (int) (vector.getY() * scaleY); return new GridPoint(x, y); }
public void inverse(Vector2D in, Vector2D out) { double[] o = inverse(in.getX(), in.getY()); out.set(o); }
public void transform(Vector2D in, Vector2D out) { double[] o = transform(in.getX(), in.getY()); out.set(o); }
/** * Rotate the polygon clockwise about a point by an arbritray angle. * * @param v Point vector to rotate about * @param angle Angle in radians */ public void rotate(Vector2D v, double angle) { rotate(v.getX(), v.getY(), angle); }
public static Vector2D suma(Vector2D a, Vector2D b) { return new Vector2D(a.getX() + b.getX(), a.getY() + b.getY()); }
public static Vector2D invertir(Vector2D a) { return new Vector2D(a.getX() * -1, a.getY() * -1); }
/** * Determines whether a vector is null. A vector with coordinates (0, 0) is null. * * @param vec the vector * @return <code>true</code> if the vector null, <code>false</code> if not */ public static boolean isNull(Vector2D vec) { return mEpsSqr > vec.getX() * vec.getX() + vec.getY() * vec.getY(); }
public static Position toPosition(Vector2D vector) { int x = Math.round(vector.getX()); int y = Math.round(vector.getY()); return new Position(x, y); }
/** * Calculates the length of a vector. * * @param vec the vector * @return the length */ public static double length(Vector2D vec) { return Math.sqrt(vec.getX() * vec.getX() + vec.getY() * vec.getY()); }
public static GridPoint toGridPoint(Vector2D vector, Vector2D scale) { float scaleX = scale.getX(); float scaleY = scale.getY(); return toGridPoint(vector, scaleX, scaleY); }
@Override public void update(DeltaState deltaState) { Vector2D newDelta = velocity.producto(deltaState.getDelta()); this.setX(this.getX() + newDelta.getX()); this.setY(this.getY() + newDelta.getY()); }
// if good - passed, if bad - ignored @Test public void studidTest() { Vector2D v = new Vector2D(2, 2); Assume.assumeTrue(v.getX() == v.getY()); }
/** * Este metodo se encarga de recibir un vector adicional y calcula el producto punto entre el * objeto y el vector recibido por el parametro * * @param b Es el vector con el que se va a multiplicar * @return El valor resultado del producto punto */ public double productoPunto(Vector2D b) { double c = x * b.getX() + y * b.getY(); return c; }
public boolean equals(Vector2D input) { if (this.x == input.getX() && this.y == input.getY()) { return true; } return false; }
public void suma(Vector2D b) { x += b.getX(); y += b.getY(); }