本文整理汇总了Java中java.awt.geom.Point2D.distance方法的典型用法代码示例。如果您正苦于以下问题:Java Point2D.distance方法的具体用法?Java Point2D.distance怎么用?Java Point2D.distance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.awt.geom.Point2D
的用法示例。
在下文中一共展示了Point2D.distance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mousePressed
import java.awt.geom.Point2D; //导入方法依赖的package包/类
public void mousePressed(MouseEvent e) {
selected = -1;
double minDist = Float.MAX_VALUE;
for (int i = 0; i < polygon.npoints; ++i) {
double dist = Point2D.distance(polygon.xpoints[i], polygon.ypoints[i], e.getX(), e.getY());
if (dist < minDist) {
minDist = dist;
selected = i;
}
}
if (e.isMetaDown()) {
polygon.addPoint(e.getX(), e.getY());
if (selected >= 0) {
for (int i = polygon.npoints - 1; i > selected; --i) {
polygon.xpoints[i] = polygon.xpoints[i - 1];
polygon.ypoints[i] = polygon.ypoints[i - 1];
}
polygon.xpoints[selected] = e.getX();
polygon.ypoints[selected] = e.getY();
}
}
}
示例2: createPointBetween
import java.awt.geom.Point2D; //导入方法依赖的package包/类
/**
* Creates an returns a point halfway two given points, with a random effect
* @param p1 the first boundary point
* @param p2 the first boundary point
* @return new point on the perpendicular of the line between <tt>p1</tt>
* and <tt>p2</tt>
*/
private Point createPointBetween(Point2D p1, Point2D p2) {
double distance = p1.distance(p2);
int midX = (int) (p1.getX() + p2.getX()) / 2;
int midY = (int) (p1.getY() + p2.getY()) / 2;
// int offset = (int) (5 + distance / 2 + 20 * Math.random());
int x, y;
if (distance == 0) {
x = midX + 20;
y = midY + 20;
} else {
int offset = (int) (5 + distance / 4);
double xDelta = p1.getX() - p2.getX();
double yDelta = p1.getY() - p2.getY();
x = midX + (int) (offset * yDelta / distance);
y = midY - (int) (offset * xDelta / distance);
}
return new Point(Math.max(x, 0), Math.max(y, 0));
}
示例3: createPointPerpendicular
import java.awt.geom.Point2D; //导入方法依赖的package包/类
/**
* Creates and returns a point perpendicular to the line between two points,
* at a distance to the second point that is a fraction of the length of the
* original line. A boolean flag controls the direction to which the
* perpendicular point sticks out from the original line.
* @param p1 the first boundary point
* @param p2 the first boundary point
* @param left flag to indicate whether the new point is to stick out on the
* left or right hand side of the line between <tt>p1</tt> and
* <tt>p2</tt>.
* @return new point on the perpendicular of the line between <tt>p1</tt>
* and <tt>p2</tt>
*/
private Point createPointPerpendicular(Point2D p1, Point2D p2, boolean left) {
double distance = p1.distance(p2);
int midX = (int) (p1.getX() + p2.getX()) / 2;
int midY = (int) (p1.getY() + p2.getY()) / 2;
// int offset = (int) (5 + distance / 2 + 20 * Math.random());
int x, y;
if (distance == 0) {
x = midX + 20;
y = midY + 20;
} else {
int offset = (int) (5 + distance / 4);
if (left) {
offset = -offset;
}
double xDelta = p1.getX() - p2.getX();
double yDelta = p1.getY() - p2.getY();
x = (int) (p2.getX() + offset * yDelta / distance);
y = (int) (p2.getY() - offset * xDelta / distance);
}
return new Point(Math.max(x, 0), Math.max(y, 0));
}
示例4: drawLine
import java.awt.geom.Point2D; //导入方法依赖的package包/类
private void drawLine(Edge e) {
Point2D from = e.getSource().getPosition();
Point2D to = e.getTarget().getPosition();
double a = Math.toDegrees(Utilities.calculateAngle(from, to));
double dist = from.distance(to);
// draw line
gl.glPushMatrix();
gl.glTranslated(from.getX(), from.getY(), 0);
gl.glScaled(dist, dist, 1);
gl.glRotated(a, 0, 0, 1);
gl.glCallList(DLIST_LINE);
gl.glPopMatrix();
if (renderer.getNetwork().isDirected()) {
// draw arrow on line
Point2D b = Utilities.getBorderPoint(e.getTarget(), from,
scaleFactor);
gl.glPushMatrix();
gl.glTranslated(b.getX(), b.getY(), 0);
gl.glRotated(a, 0, 0, 1);
gl.glScaled(1 / scaleFactor, 1 / scaleFactor, 1);
gl.glCallList(DLIST_ARROW);
gl.glPopMatrix();
}
}
示例5: handleForces
import java.awt.geom.Point2D; //导入方法依赖的package包/类
private void handleForces() {
// clean up forces
this.activeForces.forEach(x -> {
if (x.hasEnded()) {
this.activeForces.remove(x);
}
});
for (final Force force : this.activeForces) {
if (force.cancelOnReached() && force.hasReached(this.getEntity())) {
force.end();
continue;
}
final Point2D collisionBoxCenter = new Point2D.Double(this.getEntity().getCollisionBox().getCenterX(), this.getEntity().getCollisionBox().getCenterY());
if (collisionBoxCenter.distance(force.getLocation()) < FORCE_APPLY_ACCEPTED_ERROR) {
final double yDelta = this.getEntity().getHeight() - this.getEntity().getCollisionBox().getHeight() + this.getEntity().getCollisionBox().getHeight() / 2;
final Point2D entityLocation = new Point2D.Double(force.getLocation().getX() - this.getEntity().getWidth() / 2, force.getLocation().getY() - yDelta);
this.getEntity().setLocation(entityLocation);
} else {
final double angle = GeometricUtilities.calcRotationAngleInDegrees(collisionBoxCenter, force.getLocation());
final boolean success = this.getPhysicsEngine().move(this.getEntity(), (float) angle, Game.getLoop().getDeltaTime() * 0.001f * force.getStrength());
if (force.cancelOnCollision() && !success) {
force.end();
}
}
}
}
示例6: encodeRadial
import java.awt.geom.Point2D; //导入方法依赖的package包/类
private String encodeRadial(Shape ps, RadialGradient g) {
float centerX1 = (float)ps.getPaintX1();
float centerY1 = (float)ps.getPaintY1();
float x2 = (float)ps.getPaintX2();
float y2 = (float)ps.getPaintY2();
float radius = (float)Point2D.distance(centerX1, centerY1, x2, y2);
StringBuilder b = new StringBuilder();
b.append(" return decodeRadialGradient((");
b.append(centerX1);
b.append("f * w) + x, (");
b.append(centerY1);
b.append("f * h) + y, ");
b.append(radius);
b.append("f,\n");
encodeGradientColorsAndFractions(g,b);
b.append(");");
String methodBody = b.toString();
String methodName = methods.get(methodBody);
if (methodName == null) {
methodName = "decodeRadial" + radialCounter++;
gradientsCode.append(" private Paint ").append(methodName).append("(Shape s) {\n");
gradientsCode.append(" Rectangle2D bounds = s.getBounds2D();\n");
gradientsCode.append(" float x = (float)bounds.getX();\n");
gradientsCode.append(" float y = (float)bounds.getY();\n");
gradientsCode.append(" float w = (float)bounds.getWidth();\n");
gradientsCode.append(" float h = (float)bounds.getHeight();\n");
gradientsCode.append(methodBody);
gradientsCode.append("\n }\n\n");
methods.put(methodBody, methodName);
}
return methodName;
}
示例7: pathLength
import java.awt.geom.Point2D; //导入方法依赖的package包/类
protected double pathLength(Point2D[] V) {
double L = 0;
final int N = V.length;
for (int i = 0; i < N; i++) {
Point2D p0 = V[i];
Point2D p1 = V[(i + 1) % N];
L = L + p0.distance(p1);
}
return L;
}
示例8: hasValidbroomLengths
import java.awt.geom.Point2D; //导入方法依赖的package包/类
/**
* Returns whether the brooms in the given configuration have valid lengths.
*
* @param cfg
* the configuration to test.
* @return whether the brooms in the given configuration have valid lengths.
*/
public boolean hasValidbroomLengths(ASVConfig cfg) {
List<Point2D> points = cfg.getASVPositions();
for (int i = 1; i < points.size(); i++) {
Point2D p0 = points.get(i - 1);
Point2D p1 = points.get(i);
double broomLength = p0.distance(p1);
if (broomLength < MIN_BROOM_LENGTH - maxError) {
return false;
} else if (broomLength > MAX_BROOM_LENGTH + maxError) {
return false;
}
}
return true;
}
示例9: updatePoints
import java.awt.geom.Point2D; //导入方法依赖的package包/类
private void updatePoints(int x, int y) {
Point2D inv = new Point2D.Double(x, y);
try {
inv = transform.inverseTransform(inv, null);
} catch (NoninvertibleTransformException e) {
e.printStackTrace();
}
x = (int)inv.getX();
y = (int)inv.getY();
switch (paintType) {
default:
case BASIC:
case LINEAR:
// pick the closest point to move
if (inv.distance(startX, startY) < inv.distance(endX, endY)) {
startX = x;
startY = y;
} else {
endX = x;
endY = y;
}
break;
case RADIAL:
// pick the closest point to move
if (inv.distance(ctrX, ctrY) < inv.distance(focusX, focusY)) {
ctrX = x;
ctrY = y;
} else {
focusX = x;
focusY = y;
}
break;
}
updatePaint();
}
示例10: getClosestIndex
import java.awt.geom.Point2D; //导入方法依赖的package包/类
/**
* Returns the positive index in a non-empty list of points of that
* point which is closest to a given location.
* @param location the location to which distances are measured.
* @param points the list in which the index is sought
* @return the index of the point (from position 1) closest to the location
*/
protected int getClosestIndex(List<Point2D> points, Point2D location) {
int result = 0;
double closestDistance = Double.MAX_VALUE;
for (int i = 1; i < points.size(); i++) {
double distance =
location.distance(points.get(i - 1)) + location.distance(points.get(i));
if (distance < closestDistance) {
result = i;
closestDistance = distance;
}
}
return result;
}
示例11: getEndPoint
import java.awt.geom.Point2D; //导入方法依赖的package包/类
/** Returns the perimeter point where the end of this edge has to connect.
* @param vertex the end vertex
* @param source if {@code true}, we're computing this for the edge source,
* otherwise for the edge target
*/
private Point2D getEndPoint(CellView vertex, boolean source) {
JVertexView vertexView = (JVertexView) vertex;
Point2D center = getCenterPoint(vertex);
Point2D nextPoint = getNearestPoint(source);
// adjust the centre and next point depending on the number of
// parallel edges, as determined by the parameter rank
Point2D adjustedCenter;
Point2D adjustedNextPoint;
int parRank = source ? getParRank() : -getParRank();
if (parRank == 0) {
adjustedCenter = center;
adjustedNextPoint = nextPoint;
} else {
// direction of the next point
double dx = nextPoint.getX() - center.getX();
double dy = nextPoint.getY() - center.getY();
// direction for the offset, perpendicular to the next point
double offDirX = dy;
double offDirY = -dx;
double offDist = center.distance(nextPoint);
// calculate vertex radius in the specified direction
Rectangle2D bounds = vertex.getBounds();
double offMax = vertexView.getCellVisuals()
.getNodeShape()
.getRadius(bounds, offDirX, offDirY);
// calculate actual offset
double offset =
Math.signum(parRank) * Math.min(PAR_EDGES_DISTANCE * Math.abs(parRank), offMax);
double offX = offset * offDirX / offDist;
double offY = offset * offDirY / offDist;
adjustedCenter = new Point2D.Double(center.getX() + offX, center.getY() + offY);
adjustedNextPoint =
new Point2D.Double(nextPoint.getX() + offX, nextPoint.getY() + offY);
}
return vertexView.getPerimeterPoint(this, adjustedCenter, adjustedNextPoint);
}
示例12: findClosestTwoIndices
import java.awt.geom.Point2D; //导入方法依赖的package包/类
/**
* Finds the nearest two track points to a Point2D double point. This method
* uses the fast local minimum search to find the nearest point, then checks
* in each direction for the next nearest point.
*
* @param p the Point2D in x,y space.
* @return a Point with the two nearest track points as x (nearest) and y
* (next nearest).
*/
public Point findClosestTwoIndices(Point2D p) {
Point idxP = new Point(-1, -1);
if (trackPoints == null) {
return idxP;
}
int idx1 = findClosestIndex(p, Float.MAX_VALUE, true); // true=fast search, starting where last one ended.
if (idx1 >= 0) {
// Find which one of the neighbors is closest
int idx2 = idx1 - 1;
if (idx2 < 0) {
idx2 = getNumPoints() - 1;
}
int idx3 = idx1 + 1;
if (idx3 >= getNumPoints()) {
idx3 = 0;
}
Point2D p2 = getPoint(idx2);
Point2D p3 = getPoint(idx3);
double dist2 = p2.distance(p);
double dist3 = p3.distance(p);
if (dist2 < dist3) {
idxP.setLocation(max(idx1, idx2), min(idx1, idx2));
} else {
idxP.setLocation(max(idx1, idx3), min(idx1, idx3));
}
}
return idxP;
}
示例13: getEdgeLength
import java.awt.geom.Point2D; //导入方法依赖的package包/类
/**
* This method calculates the energy due to the length of the specified
* edge. The energy is proportional to the length of the edge, making
* shorter edges preferable in the layout.
*
* @param i the index of the edge in the array <code>e</code>
* @return the total edge length energy of the specified edge
*/
protected double getEdgeLength(int i)
{
if (isOptimizeEdgeLength)
{
double edgeLength = Point2D.distance(v[e[i].source].x,
v[e[i].source].y, v[e[i].target].x, v[e[i].target].y);
return (edgeLengthCostFactor * edgeLength * edgeLength);
}
else
{
return 0.0;
}
}
示例14: findDistanceToTrack
import java.awt.geom.Point2D; //导入方法依赖的package包/类
/**
* Returns the approximate normal distance of a point from the track. From
* <a
* href="http://www.tpub.com/math2/8.htm">http://www.tpub.com/math2/8.htm</a>
* and <a
* href="http://softsurfer.com/Archive/algorithm_0102/algorithm_0102.htm#Distance%20to%202-Point%20Line">http://softsurfer.com/Archive/algorithm_0102/algorithm_0102.htm#Distance%20to%202-Point%20Line</a>
*
* @param pos the x,y position of interest.
* @param closestIdx the closest track point, found previously.
* @return the distance from the track in pixels using the nearest two track
* points.
*/
public float findDistanceToTrack(Point2D pos, int closestIdx) {
int aIdx = -1, bIdx = -1;
if (closestIdx >= 0) {
// Find which one of the neighbors is closest
int idx2 = closestIdx - 1;
if (idx2 < 0) {
idx2 = getNumPoints() - 1;
}
int idx3 = closestIdx + 1;
if (idx3 >= getNumPoints()) {
idx3 = 0;
}
Point2D p2 = getPoint(idx2);
Point2D p3 = getPoint(idx3);
double dist2 = p2.distance(pos);
double dist3 = p3.distance(pos);
if (dist2 < dist3) {
aIdx = max(closestIdx, idx2);
bIdx = min(closestIdx, idx2);
} else {
aIdx = max(closestIdx, idx3);
bIdx = min(closestIdx, idx3);
}
}
Point2D a = getPoint(aIdx);
Point2D b = getPoint(bIdx);
Point2D vl = new Point2D.Double(b.getX() - a.getX(), b.getY() - a.getY()); // vector pointing from a to b
Point2D w = new Point2D.Double(pos.getX() - a.getX(), pos.getY() - a.getY()); // vector pointing from a to pos
double vlnorm = vl.distance(0, 0); // length of vl
double cross = Math.abs((vl.getX() * w.getY()) - (vl.getY() * w.getX()));
double dist = cross / vlnorm;
return (float) dist;
}
示例15: findClosestTwoPointsForInsert
import java.awt.geom.Point2D; //导入方法依赖的package包/类
/** Finds the nearest two track points to a point for insertion of a new track point.
*
* @param p the Point to test for. This Point has int x and y values.
* @return a Point with the indices of the two points as x and y values.
*/
private Point findClosestTwoPointsForInsert(Point p) {
Point idxP = new Point(-1, -1);
if (extractedTrack == null) {
return idxP;
}
int idx1 = extractedTrack.findClosestIndex(p, Float.MAX_VALUE, false);
if (idx1 >= 0) {
// Find which one of the neighbors is closest
int idx2 = idx1 - 1;
if (idx2 < 0) {
idx2 = extractedTrack.getNumPoints() - 1;
}
int idx3 = idx1 + 1;
if (idx3 >= extractedTrack.getNumPoints()) {
idx3 = 0;
}
Point2D p2 = extractedTrack.getPoint(idx2);
Point2D p3 = extractedTrack.getPoint(idx3);
double dist2 = p2.distance(p);
double dist3 = p3.distance(p);
if (dist2 < dist3) {
idxP.setLocation(max(idx1, idx2), min(idx1, idx2));
} else {
idxP.setLocation(max(idx1, idx3), min(idx1, idx3));
}
}
return idxP;
}