下面列出了java.awt.geom.Line2D#getY1 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates a new line by extending an existing line.
*
* @param line the line (<code>null</code> not permitted).
* @param startPercent the amount to extend the line at the start point
* end.
* @param endPercent the amount to extend the line at the end point end.
*
* @return A new line.
*
* @since 1.0.18
*/
public static Line2D extendLine(Line2D line, double startPercent,
double endPercent) {
ParamChecks.nullNotPermitted(line, "line");
double x1 = line.getX1();
double x2 = line.getX2();
double deltaX = x2 - x1;
double y1 = line.getY1();
double y2 = line.getY2();
double deltaY = y2 - y1;
x1 = x1 - (startPercent * deltaX);
y1 = y1 - (startPercent * deltaY);
x2 = x2 + (endPercent * deltaX);
y2 = y2 + (endPercent * deltaY);
return new Line2D.Double(x1, y1, x2, y2);
}
/**
* Creates a new line by extending an existing line.
*
* @param line the line (<code>null</code> not permitted).
* @param startPercent the amount to extend the line at the start point
* end.
* @param endPercent the amount to extend the line at the end point end.
*
* @return A new line.
*/
private Line2D extendLine(Line2D line, double startPercent,
double endPercent) {
if (line == null) {
throw new IllegalArgumentException("Null 'line' argument.");
}
double x1 = line.getX1();
double x2 = line.getX2();
double deltaX = x2 - x1;
double y1 = line.getY1();
double y2 = line.getY2();
double deltaY = y2 - y1;
x1 = x1 - (startPercent * deltaX);
y1 = y1 - (startPercent * deltaY);
x2 = x2 + (endPercent * deltaX);
y2 = y2 + (endPercent * deltaY);
return new Line2D.Double(x1, y1, x2, y2);
}
/**
* Sets the parameters of this description object to match the supplied object.
*
* @param o
* the object (should be an instance of <code>Line2D</code>).
* @throws ObjectFactoryException
* if the object is not an instance of <code>Line2D</code>.
*/
public void setParameterFromObject( final Object o ) throws ObjectFactoryException {
if ( !( o instanceof Line2D ) ) {
throw new ObjectFactoryException( "The given object is no java.awt.geom.Line2D." );
}
final Line2D line = (Line2D) o;
final float x1 = (float) line.getX1();
final float x2 = (float) line.getX2();
final float y1 = (float) line.getY1();
final float y2 = (float) line.getY2();
setParameter( "x1", new Float( x1 ) );
setParameter( "x2", new Float( x2 ) );
setParameter( "y1", new Float( y1 ) );
setParameter( "y2", new Float( y2 ) );
}
/**
* This method calculates intersections of two lines.
*
* @param a
* Line 1
* @param b
* Line 2
* @return Intersection point
*/
private static Point getLinesIntersection(Line2D a, Line2D b) {
double d = (a.getX1() - a.getX2()) * (b.getY2() - b.getY1()) - (a.getY1() - a.getY2()) * (b.getX2() - b.getX1());
double da = (a.getX1() - b.getX1()) * (b.getY2() - b.getY1()) - (a.getY1() - b.getY1()) * (b.getX2() - b.getX1());
double ta = da / d;
Point p = new Point();
p.setLocation(a.getX1() + ta * (a.getX2() - a.getX1()), a.getY1() + ta * (a.getY2() - a.getY1()));
return p;
}
public static double sin(Line2D aLine) {
final double length = getLineLength(aLine.getX1(), aLine.getY1(), aLine.getX2(), aLine.getY2());
if (length == 0) {
throw new IllegalArgumentException(toString(aLine) + " has a zero length");
}
double deltaY = aLine.getY2() - aLine.getY1();
return deltaY / length;
}
/**
* This method calculates intersections of two lines.
* @param a Line 1
* @param b Line 2
* @return Intersection point
*/
private static Point getLinesIntersection(Line2D a, Line2D b) {
double d = (a.getX1()-a.getX2())*(b.getY2()-b.getY1()) - (a.getY1()-a.getY2())*(b.getX2()-b.getX1());
double da = (a.getX1()-b.getX1())*(b.getY2()-b.getY1()) - (a.getY1()-b.getY1())*(b.getX2()-b.getX1());
// double db = (a.getX1()-a.getX2())*(a.getY1()-b.getY1()) - (a.getY1()-a.getY2())*(a.getX1()-b.getX1());
double ta = da/d;
// double tb = db/d;
Point p = new Point();
p.setLocation(a.getX1()+ta*(a.getX2()-a.getX1()), a.getY1()+ta*(a.getY2()-a.getY1()));
return p;
}
/**
*
* @param line1
* @param line2
* @param extrapolate
* @return
*/
public static Point2D getIntersectionPoint(Line2D line1, Line2D line2,
boolean extrapolate) {
if (extrapolate || line1.intersectsLine(line2)) {
float x1 = (float) line2.getX1();
float y1 = (float) line2.getY1();
float x2 = (float) line2.getX2();
float y2 = (float) line2.getY2();
float xp1 = (float) line1.getX1();
float yp1 = (float) line1.getY1();
float xp2 = (float) line1.getX2();
float yp2 = (float) line1.getY2();
float y = 0;
float x = 0;
float dy = y2 - y1;
float s = (x2 - x1) / dy;
float dpy = yp2 - yp1;
float sp = (xp2 - xp1) / dpy;
if (y1 == y2) {
if (dpy == 0) {
return null;
}
y = y1;
x = xp1 + sp * (y - yp1);
} else if (yp1 == yp2) {
if (dy == 0) {
return null;
}
y = yp1;
x = x1 + s * (y - y1);
} else {
if (dy == 0 || dpy == 0 || (s - sp) == 0) {
return null;
}
y = (xp1 - x1 + s * y1 - sp * yp1) / (s - sp);
x = x1 + s * (y - y1);
}
return new Point2D.Float(x, y);
}
return null;
}
private void renderGlyphTranslations (Glyph glyph,
Graphics2D g)
{
if (glyph.getTranslations().isEmpty()) {
return;
}
Stroke oldStroke = UIUtil.setAbsoluteStroke(g, 1f);
Color oldColor = g.getColor();
g.setColor(Colors.TRANSLATION_LINK);
// Compute end radius, with fixed size whatever the current zoom
double r = 1 / g.getTransform().getScaleX();
for (PartNode node : glyph.getTranslations()) {
for (Line2D line : node.getTranslationLinks(glyph)) {
// Draw line
g.draw(line);
// Draw ending points
Ellipse2D e1 = new Ellipse2D.Double(
line.getX1() - r, line.getY1() - r, 2 * r, 2 * r);
g.draw(e1);
Ellipse2D e2 = new Ellipse2D.Double(
line.getX2() - r, line.getY2() - r, 2 * r, 2 * r);
g.draw(e2);
}
}
g.setColor(oldColor);
g.setStroke(oldStroke);
}
/** Converts line's bounds by the bounds of the root pane. Drop glass pane
* is over this root pane. After covert a given line is set to drop glass pane.
* @param line line for show in drop glass pane */
private void convertBoundsAndSetDropLine(final Line2D line) {
int x1 = (int) line.getX1();
int x2 = (int) line.getX2();
int y1 = (int) line.getY1();
int y2 = (int) line.getY2();
Point p1 = SwingUtilities.convertPoint(table, x1, y1, table.getRootPane());
Point p2 = SwingUtilities.convertPoint(table, x2, y2, table.getRootPane());
line.setLine(p1, p2);
dropPane.setDropLine(line);
}
/**
* Creates a region surrounding a line segment by 'widening' the line
* segment. A typical use for this method is the creation of a
* 'clickable' region for a line that is displayed on-screen.
*
* @param line the line (<code>null</code> not permitted).
* @param width the width of the region.
*
* @return A region that surrounds the line.
*/
public static Shape createLineRegion(Line2D line, float width) {
GeneralPath result = new GeneralPath();
float x1 = (float) line.getX1();
float x2 = (float) line.getX2();
float y1 = (float) line.getY1();
float y2 = (float) line.getY2();
if ((x2 - x1) != 0.0) {
double theta = Math.atan((y2 - y1) / (x2 - x1));
float dx = (float) Math.sin(theta) * width;
float dy = (float) Math.cos(theta) * width;
result.moveTo(x1 - dx, y1 + dy);
result.lineTo(x1 + dx, y1 - dy);
result.lineTo(x2 + dx, y2 - dy);
result.lineTo(x2 - dx, y2 + dy);
result.closePath();
}
else {
// special case, vertical line
result.moveTo(x1 - width / 2.0f, y1);
result.lineTo(x1 + width / 2.0f, y1);
result.lineTo(x2 + width / 2.0f, y2);
result.lineTo(x2 - width / 2.0f, y2);
result.closePath();
}
return result;
}
public int getNearestSegment(Point2D pt, Point2D outPointOnSegment) {
double min = Double.MAX_VALUE;
int nearest = -1;
for (int i = 0; i < getSegmentCount(); i++) {
Line2D segment = getSegment(i);
Point2D a = new Point2D.Double(pt.getX() - segment.getX1(), pt.getY() - segment.getY1());
Point2D b = new Point2D.Double(segment.getX2() - segment.getX1(), segment.getY2() - segment.getY1());
double magB = b.distance(0, 0);
double dist;
if (magB < 0.0000001) {
dist = pt.distance(segment.getP1());
} else {
b.setLocation(b.getX() / magB, b.getY() / magB);
double magAonB = a.getX() * b.getX() + a.getY() * b.getY();
if (magAonB < 0) {
magAonB = 0;
}
if (magAonB > magB) {
magAonB = magB;
}
a.setLocation(segment.getX1() + b.getX() * magAonB, segment.getY1() + b.getY() * magAonB);
dist = new Point2D.Double(pt.getX() - a.getX(), pt.getY() - a.getY()).distance(0, 0);
}
if (dist < min) {
min = dist;
if (outPointOnSegment != null) {
outPointOnSegment.setLocation(a);
}
nearest = i;
}
}
return nearest;
}
@Override
public Point2D getEndVector (boolean reverse)
{
int dir = reverse ? (-1) : 1;
Line2D l = line.toDouble();
double length = l.getP1().distance(l.getP2());
return new Point2D.Double(
(dir * (l.getX2() - l.getX1())) / length,
(dir * (l.getY2() - l.getY1())) / length);
}
/**
* Report the middle point of a Line2D.
*
* @param line provided line2D
* @return the middle point between p1 and p2
*/
public static Point2D middle (Line2D line)
{
return new Point2D.Double(
(line.getX1() + line.getX2()) / 2.0,
(line.getY1() + line.getY2()) / 2.0);
}
/**
* Creates a region surrounding a line segment by 'widening' the line
* segment. A typical use for this method is the creation of a
* 'clickable' region for a line that is displayed on-screen.
*
* @param line the line (<code>null</code> not permitted).
* @param width the width of the region.
*
* @return A region that surrounds the line.
*/
public static Shape createLineRegion(Line2D line, float width) {
GeneralPath result = new GeneralPath();
float x1 = (float) line.getX1();
float x2 = (float) line.getX2();
float y1 = (float) line.getY1();
float y2 = (float) line.getY2();
if ((x2 - x1) != 0.0) {
double theta = Math.atan((y2 - y1) / (x2 - x1));
float dx = (float) Math.sin(theta) * width;
float dy = (float) Math.cos(theta) * width;
result.moveTo(x1 - dx, y1 + dy);
result.lineTo(x1 + dx, y1 - dy);
result.lineTo(x2 + dx, y2 - dy);
result.lineTo(x2 - dx, y2 + dy);
result.closePath();
}
else {
// special case, vertical line
result.moveTo(x1 - width / 2.0f, y1);
result.lineTo(x1 + width / 2.0f, y1);
result.lineTo(x2 + width / 2.0f, y2);
result.lineTo(x2 - width / 2.0f, y2);
result.closePath();
}
return result;
}
/**
* Helper method to retrieve StemPortion of the connection.
*
* @param head the item connected to the stem (head)
* @param stemLine logical range of the stem
* @param yExtension ordinate of head-stem extension point
* @return the stem Portion
*/
public static StemPortion getStemPortion (HeadInter head,
Line2D stemLine,
double yExtension)
{
final double margin = head.getBounds().height * constants.anchorHeightRatio.getValue();
final double yMidStem = (stemLine.getY1() + stemLine.getY2()) / 2;
if (yExtension >= yMidStem) {
return (yExtension > (stemLine.getY2() - margin)) ? STEM_BOTTOM : STEM_MIDDLE;
} else {
return (yExtension < (stemLine.getY1() + margin)) ? STEM_TOP : STEM_MIDDLE;
}
}
/**
* Calculates the anchor point for a label.
*
* @param line the line for the crosshair.
* @param anchor the anchor point.
* @param deltaX the x-offset.
* @param deltaY the y-offset.
*
* @return The anchor point.
*/
private Point2D calculateLabelPoint(Line2D line, RectangleAnchor anchor,
double deltaX, double deltaY) {
double x, y;
boolean left = (anchor == RectangleAnchor.BOTTOM_LEFT
|| anchor == RectangleAnchor.LEFT
|| anchor == RectangleAnchor.TOP_LEFT);
boolean right = (anchor == RectangleAnchor.BOTTOM_RIGHT
|| anchor == RectangleAnchor.RIGHT
|| anchor == RectangleAnchor.TOP_RIGHT);
boolean top = (anchor == RectangleAnchor.TOP_LEFT
|| anchor == RectangleAnchor.TOP
|| anchor == RectangleAnchor.TOP_RIGHT);
boolean bottom = (anchor == RectangleAnchor.BOTTOM_LEFT
|| anchor == RectangleAnchor.BOTTOM
|| anchor == RectangleAnchor.BOTTOM_RIGHT);
Rectangle rect = line.getBounds();
// we expect the line to be vertical or horizontal
if (line.getX1() == line.getX2()) { // vertical
x = line.getX1();
y = (line.getY1() + line.getY2()) / 2.0;
if (left) {
x = x - deltaX;
}
if (right) {
x = x + deltaX;
}
if (top) {
y = Math.min(line.getY1(), line.getY2()) + deltaY;
}
if (bottom) {
y = Math.max(line.getY1(), line.getY2()) - deltaY;
}
}
else { // horizontal
x = (line.getX1() + line.getX2()) / 2.0;
y = line.getY1();
if (left) {
x = Math.min(line.getX1(), line.getX2()) + deltaX;
}
if (right) {
x = Math.max(line.getX1(), line.getX2()) - deltaX;
}
if (top) {
y = y - deltaY;
}
if (bottom) {
y = y + deltaY;
}
}
return new Point2D.Double(x, y);
}
public static boolean isContent( final RenderBox element, final boolean ellipseAsBackground,
final boolean shapesAsContent ) {
// For legacy reasons: A single ReplacedContent in a paragraph means, we may have a old-style border and
// background definition.
if ( element.getNodeType() == LayoutNodeTypes.TYPE_BOX_CONTENT ) {
final RenderableReplacedContentBox contentBox = (RenderableReplacedContentBox) element;
final RenderableReplacedContent rpc = contentBox.getContent();
final Object rawContentObject = rpc.getRawObject();
if ( rawContentObject instanceof DrawableWrapper == false ) {
return true;
}
final DrawableWrapper wrapper = (DrawableWrapper) rawContentObject;
final Object rawbackend = wrapper.getBackend();
if ( rawbackend instanceof ShapeDrawable == false ) {
return true;
}
final ShapeDrawable drawable = (ShapeDrawable) rawbackend;
final Shape rawObject = drawable.getShape();
final StyleSheet styleSheet = element.getStyleSheet();
if ( shapesAsContent == false ) {
return false;
}
if ( rawObject instanceof Line2D ) {
if ( hasBorderEdge( styleSheet ) ) {
final Line2D line = (Line2D) rawObject;
if ( line.getY1() == line.getY2() ) {
return false;
} else if ( line.getX1() == line.getX2() ) {
return false;
}
}
} else if ( rawObject instanceof Rectangle2D ) {
return false;
} else if ( ellipseAsBackground && rawObject instanceof Ellipse2D ) {
return false;
} else if ( rawObject instanceof RoundRectangle2D ) {
return false;
}
return true;
}
RenderNode child = element.getFirstChild();
while ( child != null ) {
final int type = child.getNodeType();
if ( ( type & LayoutNodeTypes.MASK_BOX_INLINE ) == LayoutNodeTypes.MASK_BOX_INLINE ) {
return true;
}
if ( type == LayoutNodeTypes.TYPE_NODE_TEXT ) {
return true;
}
child = child.getNext();
}
return false;
}
/**
* Clips the specified line to the given rectangle.
*
* @param line the line (<code>null</code> not permitted).
* @param rect the clipping rectangle (<code>null</code> not permitted).
*
* @return <code>true</code> if the clipped line is visible, and
* <code>false</code> otherwise.
*/
public static boolean clipLine(Line2D line, Rectangle2D rect) {
double x1 = line.getX1();
double y1 = line.getY1();
double x2 = line.getX2();
double y2 = line.getY2();
double minX = rect.getMinX();
double maxX = rect.getMaxX();
double minY = rect.getMinY();
double maxY = rect.getMaxY();
int f1 = rect.outcode(x1, y1);
int f2 = rect.outcode(x2, y2);
while ((f1 | f2) != 0) {
if ((f1 & f2) != 0) {
return false;
}
double dx = (x2 - x1);
double dy = (y2 - y1);
// update (x1, y1), (x2, y2) and f1 and f2 using intersections
// then recheck
if (f1 != 0) {
// first point is outside, so we update it against one of the
// four sides then continue
if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
&& dx != 0.0) {
y1 = y1 + (minX - x1) * dy / dx;
x1 = minX;
}
else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
&& dx != 0.0) {
y1 = y1 + (maxX - x1) * dy / dx;
x1 = maxX;
}
else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
&& dy != 0.0) {
x1 = x1 + (maxY - y1) * dx / dy;
y1 = maxY;
}
else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
&& dy != 0.0) {
x1 = x1 + (minY - y1) * dx / dy;
y1 = minY;
}
f1 = rect.outcode(x1, y1);
}
else if (f2 != 0) {
// second point is outside, so we update it against one of the
// four sides then continue
if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
&& dx != 0.0) {
y2 = y2 + (minX - x2) * dy / dx;
x2 = minX;
}
else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
&& dx != 0.0) {
y2 = y2 + (maxX - x2) * dy / dx;
x2 = maxX;
}
else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
&& dy != 0.0) {
x2 = x2 + (maxY - y2) * dx / dy;
y2 = maxY;
}
else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
&& dy != 0.0) {
x2 = x2 + (minY - y2) * dx / dy;
y2 = minY;
}
f2 = rect.outcode(x2, y2);
}
}
line.setLine(x1, y1, x2, y2);
return true; // the line is visible - if it wasn't, we'd have
// returned false from within the while loop above
}
/**
* Compute the intersection of two line segments.
* @param a the first line segment
* @param b the second line segment
* @param intersect a Point in which to store the intersection point
* @return the intersection code. One of {@link #NO_INTERSECTION},
* {@link #COINCIDENT}, or {@link #PARALLEL}.
*/
public static int intersectLineLine(Line2D a, Line2D b, Point2D intersect) {
double a1x = a.getX1(), a1y = a.getY1();
double a2x = a.getX2(), a2y = a.getY2();
double b1x = b.getX1(), b1y = b.getY1();
double b2x = b.getX2(), b2y = b.getY2();
return intersectLineLine(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y, intersect);
}
/**
* Report the slope of provided line.
* Line is expected not to be vertical
*
* @param line the provided line
* @return tangent of angle with horizontal
*/
public static double getSlope (Line2D line)
{
return (line.getY2() - line.getY1()) / (line.getX2() - line.getX1());
}