Java Code Examples for java.awt.geom.Line2D#relativeCCW()

The following examples show how to use java.awt.geom.Line2D#relativeCCW() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HeadInter.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean contains (Point point)
{
    if (!super.contains(point)) {
        return false;
    }

    Line2D midLine = getMidLine();

    if (midLine != null) {
        return midLine.relativeCCW(point) < 0;
    }

    return true;
}
 
Example 2
Source File: Utils2D.java    From orson-charts with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates and returns a line that is perpendicular to the specified line.
 *
 * @param line  the reference line ({@code null} not permitted).
 * @param b  the base point, expressed as a percentage along the length of 
 *     the reference line.
 * @param size  the size or length of the perpendicular line.
 * @param opposingPoint  an opposing point, to define which side of the 
 *     reference line the perpendicular line will extend ({@code null} 
 *     not permitted).
 *
 * @return The perpendicular line.
 */
public static Line2D createPerpendicularLine(Line2D line, double b, 
        double size, Point2D opposingPoint) {
    double dx = line.getX2() - line.getX1();
    double dy = line.getY2() - line.getY1();
    double length = Math.sqrt(dx * dx + dy * dy);
    double pdx = dy / length;
    double pdy = -dx / length;
    int ccw = line.relativeCCW(opposingPoint);
    Point2D pt1 = new Point2D.Double(line.getX1() + b * dx, 
            line.getY1() + b * dy);
    Point2D pt2 = new Point2D.Double(pt1.getX() - ccw * size * pdx, 
            pt1.getY() - ccw * size * pdy);
    return new Line2D.Double(pt1, pt2);
}
 
Example 3
Source File: Intersections.java    From pumpernickel with MIT License 4 votes vote down vote up
/** Defines a polygon for a cubic curve. */
private static int definePolygon(double[] array, double x0, double y0,
		double cx0, double cy0, double cx1, double cy1, double x1, double y1) {
	// first check to see if a triangle will do, instead of a quad:
	array[0] = x0;
	array[1] = y0;
	array[2] = cx1;
	array[3] = cy1;
	array[4] = x1;
	array[5] = y1;
	if (polygonContains(cx0, cy0, array, 3)) {
		return 3;
	}
	array[2] = cx0;
	array[3] = cy0;
	if (polygonContains(cx1, cy1, array, 3)) {
		return 3;
	}
	array[4] = cx1;
	array[5] = cy1;
	if (polygonContains(x1, y1, array, 3)) {
		return 3;
	}
	array[0] = x1;
	array[1] = y1;
	if (polygonContains(x0, y0, array, 3)) {
		return 3;
	}

	if (Line2D.relativeCCW(x0, y0, x1, y1, cx0, cy0) == Line2D.relativeCCW(
			x0, y0, x1, y1, cx1, cy1)) {
		if (Line2D.linesIntersect(x0, y0, cx0, cy0, cx1, cy1, x1, y1)) {
			array[0] = x0;
			array[1] = y0;
			array[2] = cx1;
			array[3] = cy1;
			array[4] = cx0;
			array[5] = cy0;
			array[6] = x1;
			array[7] = y1;
			return 4;
		}
		array[0] = x0;
		array[1] = y0;
		array[2] = cx0;
		array[3] = cy0;
		array[4] = cx1;
		array[5] = cy1;
		array[6] = x1;
		array[7] = y1;
		return 4;
	} else {
		array[0] = x0;
		array[1] = y0;
		array[2] = cx0;
		array[3] = cy0;
		array[4] = x1;
		array[5] = y1;
		array[6] = cx1;
		array[7] = cy1;
		return 4;
	}
}
 
Example 4
Source File: mxGraphView.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the relative point that describes the given, absolute label
 * position for the given edge state.
 */
public mxPoint getRelativePoint(mxCellState edgeState, double x, double y) {
  mxIGraphModel model = graph.getModel();
  mxGeometry geometry = model.getGeometry(edgeState.getCell());

  if (geometry != null) {
    int pointCount = edgeState.getAbsolutePointCount();

    if (geometry.isRelative() && pointCount > 1) {
      double totalLength = edgeState.getLength();
      double[] segments = edgeState.getSegments();

      // Works which line segment the point of the label is closest to
      mxPoint p0 = edgeState.getAbsolutePoint(0);
      mxPoint pe = edgeState.getAbsolutePoint(1);
      Line2D line = new Line2D.Double(p0.getPoint(), pe.getPoint());
      double minDist = line.ptSegDistSq(x, y);

      int index = 0;
      double tmp = 0;
      double length = 0;

      for (int i = 2; i < pointCount; i++) {
        tmp += segments[i - 2];
        pe = edgeState.getAbsolutePoint(i);

        line = new Line2D.Double(p0.getPoint(), pe.getPoint());
        double dist = line.ptSegDistSq(x, y);

        if (dist < minDist) {
          minDist = dist;
          index = i - 1;
          length = tmp;
        }

        p0 = pe;
      }

      double seg = segments[index];
      p0 = edgeState.getAbsolutePoint(index);
      pe = edgeState.getAbsolutePoint(index + 1);

      double x2 = p0.getX();
      double y2 = p0.getY();

      double x1 = pe.getX();
      double y1 = pe.getY();

      double px = x;
      double py = y;

      double xSegment = x2 - x1;
      double ySegment = y2 - y1;

      px -= x1;
      py -= y1;
      double projlenSq = 0;

      px = xSegment - px;
      py = ySegment - py;
      double dotprod = px * xSegment + py * ySegment;

      if (dotprod <= 0.0) {
        projlenSq = 0;
      }
      else {
        projlenSq = dotprod * dotprod / (xSegment * xSegment + ySegment * ySegment);
      }

      double projlen = Math.sqrt(projlenSq);

      if (projlen > seg) {
        projlen = seg;
      }

      double yDistance = Line2D.ptLineDist(p0.getX(), p0.getY(), pe.getX(), pe.getY(), x, y);
      int direction = Line2D.relativeCCW(p0.getX(), p0.getY(), pe.getX(), pe.getY(), x, y);

      if (direction == -1) {
        yDistance = -yDistance;
      }

      // Constructs the relative point for the label
      return new mxPoint(Math.round(((totalLength / 2 - length - projlen) / totalLength) * -2), Math.round(yDistance / scale));
    }
  }

  return new mxPoint();
}
 
Example 5
Source File: Utils2D.java    From orson-charts with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Creates and returns a line that is perpendicular to the specified 
 * line.
 * 
 * @param line  the reference line ({@code null} not permitted).
 * @param pt1  a point on the reference line ({@code null} not 
 *     permitted).
 * @param size  the length of the new line.
 * @param opposingPoint  an opposing point, to define which side of the 
 *     reference line the perpendicular line will extend ({@code null} 
 *     not permitted).
 * 
 * @return The perpendicular line. 
 */
public static Line2D createPerpendicularLine(Line2D line, Point2D pt1, 
        double size, Point2D opposingPoint) {
    double dx = line.getX2() - line.getX1();
    double dy = line.getY2() - line.getY1();
    double length = Math.sqrt(dx * dx + dy * dy);
    double pdx = dy / length;
    double pdy = -dx / length;
    int ccw = line.relativeCCW(opposingPoint);
    Point2D pt2 = new Point2D.Double(pt1.getX() - ccw * size * pdx, 
            pt1.getY() - ccw * size * pdy);
    return new Line2D.Double(pt1, pt2);
}