jfpoint.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 776 行 · 第 1/2 页
JAVA
776 行
/**
* $Id:JFPoint.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfgraph.geom;
import com.jfimagine.jfgraph.geom.Angle;
import com.jfimagine.jfgraph.geom.JFVector;
/**
* JFPoint class. A class used to represent a point in the plane with double coordinates.
*
* <p> Attention: Here we used a clockwise quadrant system.
* And the first quadrant is under right bottom corner.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class JFPoint implements Cloneable {
/**
* x coordinate.
*/
private double m_x =0;
/**
* y coordinate.
*/
private double m_y =0;
/**
* Get x coordiate of this point.
*
* @return The x coordiate.
*
*/
public double getX(){
return m_x;
}
/**
* Set x coordiate of this point.
*
* @param val A new x coordiate.
*
*/
public void setX(double val){
m_x =val;
}
/**
* Get y coordiate of this point.
*
* @return The y coordiate.
*
*/
public double getY(){
return m_y;
}
/**
* Set y coordiate of this point.
*
* @param val A new y coordiate.
*
*/
public void setY(double val){
m_y =val;
}
/**
* Set value of current JFPoint.
*
* @param val A new JFPoint object.
*
*/
public void setValue(JFPoint pnt){
if (pnt!=null)
setValue(pnt.getX(),pnt.getY());
}
/**
* Set value of current JFPoint.
*
* @param x, y A new JFPoint coordinates.
*
*/
public void setValue(double x, double y){
m_x =x;
m_y =y;
}
/**
* Constructor for JFPoint.
* Default to 0 for x and y coordinates.
*
*/
public JFPoint(){
m_x =0;
m_y =0;
}
/**
* Constructor for JFPoint.
*
* @param x X coordiate.
*
* @param y Y coordiate.
*
*/
public JFPoint(double x, double y){
m_x =x;
m_y =y;
}
/**
* Constructor for JFPoint.
*
* @param pnt A JFPoint.
*
*/
public JFPoint(JFPoint pnt){
if (pnt!=null){
m_x =pnt.getX();
m_y =pnt.getY();
}
}
/**
* Test if this point has valid values.
* We set an invalid point at GeomConst.LARGE_VALUE,GeomConst.LARGE_VALUE.
*/
public boolean isValid(){
return (m_x!=GeomConst.LARGE_VALUE && m_y!=GeomConst.LARGE_VALUE);
}
/**
* Test if a point is close to this point.
* Here we used an analog offset for 'pick' this line.
*
* @param pnt A point to be measured.
*
* @param pickOffset An analog offset for 'pick' this line.
*
* @return True if the point is close to this line, false otherwise.
*
*/
public boolean contains(JFPoint pnt,double pickOffset){
return distance(pnt)<=pickOffset;
}
/**
* Test if a point is close to this point.
* Here we used an analog offset for 'pick' this line.
*
* @param x, y A point to be measured.
*
* @param pickOffset An analog offset for 'pick' this line.
*
* @return True if the point is close to this line, false otherwise.
*
*/
public boolean contains(double x, double y,double pickOffset){
return distance(x,y)<=pickOffset;
}
/**
* Get the distance from this point to a specific point.
*
* @param pnt An end point to measure distance.
*
* @return the distance.
*
*/
public double distance(JFPoint pnt){
if (pnt==null)
return 0;
else{
return distance(pnt.getX(),pnt.getY());
}
}
/**
* Get the distance from this point to a specific point(use x & y coordiates for instead).
*
* @param x X coordiate of an end point to measure distance.
*
* @param y Y coordiate of an end point to measure distance.
*
* @return the distance.
*
*/
public double distance(double x, double y){
return Math.sqrt(Math.pow((x - m_x),2)+ Math.pow((y - m_y),2));
}
/**
* Get the middle point between this point and a specific point
*
* @param pnt An end point to get middle point.
*
* @return the middle point.
*
*/
public JFPoint midPoint(JFPoint pnt){
if (pnt==null)
return new JFPoint();
else
return midPoint(pnt.getX(),pnt.getY());
}
/**
* Get the middle point between this point and a specific point(use x & y coordiates for instead).
*
* @param x X coordiate of an end point to measure distance.
*
* @param y Y coordiate of an end point to measure distance.
*
* @return the middle point.
*
*/
public JFPoint midPoint(double x, double y){
return new JFPoint( (x+m_x)/2, (y+m_y)/2 );
}
/**
* Test if current point is at the position between other two points.
* Assume that the three points are on a same line.
*
* @param point1 First point.
*
* @param point2 Second point.
*
* @return If current point is at middle of other ones, return true, otherwise false.
*
*/
public boolean middleOf(JFPoint point1,JFPoint point2){
return middleOf(point1.getX(),point1.getY(),point2.getX(),point2.getY());
}
/**
* Test if current point is at the position between other two points.
* Assume that the three points are on a same line.
*
* @param x1, y1 First point.
*
* @param x2, y2 Second point.
*
* @return If current point is at middle of other ones, return true, otherwise false.
*
*/
public boolean middleOf(double x1, double y1,double x2,double y2){
return
((x1<x2 && m_x>=x1 && m_x<=x2) || (x1>=x2 && m_x>=x2 && m_x<=x1)) &&
((y1<y2 && m_y>=y1 && m_y<=y2) || (y1>=y2 && m_y>=y2 && m_y<=y1));
}
/**
* Get a point on the line from this point to specified point,
* close to this point and with the given distance.
*
* @param pnt An end point to decide a line.
*
* @param dist A distance from the wanted point to this point.
*
* @return the point.
*
*/
public JFPoint nearPoint(JFPoint pnt,double dist){
return nearPoint(pnt,dist,false);
}
/**
* Get a point on the line from this point to specified point,
* close to this point and with the given distance.
*
* @param pnt An end point to decide a line.
*
* @param dist A distance from the wanted point to this point.
*
* @param allowExtend Allow extend the line to get a near point.
*
* @return the point.
*
*/
public JFPoint nearPoint(JFPoint pnt,double dist, boolean allowExtend){
if (pnt==null || dist<0)
return null;
if (equals(pnt))
return new JFPoint(pnt);
//vertical line from this point to specified point.
if ((float)getX()==(float)pnt.getX()){
if (pnt.getY()>getY()){
return new JFPoint(pnt.getX(),getY()+dist);
}else{
return new JFPoint(pnt.getX(),getY()-dist);
}
}
//slope of the line from this point to specified point.
double slope =(pnt.getY()-getY())/(pnt.getX()-getX());
//for a line from x1,y1 to x2,y2,with a slope k,will have equation as below,
//y=k(x-x1)+y1
//for a point close to x1,y1 and on the line from x1,y1 to x2,y2, with distance A,
//will have equation according to Pythagorean proposition as below,
//(y-y1)^2+(x-x1)^2=A^2
//so we get x=x1+sqrt(A^2/(1+k^2)) or x=x1-sqrt(A^2/(1+k^2))
double x1=getX()+Math.sqrt(dist*dist/(1+slope*slope));
double x2=getX()-Math.sqrt(dist*dist/(1+slope*slope));
/*
double y=0;
if (x1>=getX() && x1<=pnt.getX() || x1>=pnt.getX() && x1<=getX()){
y =slope * (x1-getX()) + getY();
return new JFPoint(x1,y);
}else if (x2>=getX() && x2<=pnt.getX() || x2>=pnt.getX() && x2<=getX()){
y =slope * (x2-getX()) + getY();
return new JFPoint(x2,y);
}else{
return new JFPoint(pnt);
}
*/
double y1=slope * (x1-getX()) + getY();
double y2=slope * (x2-getX()) + getY();
JFPoint pnt1 =new JFPoint(x1,y1);
JFPoint pnt2 =new JFPoint(x2,y2);
if (!allowExtend){
if (pnt1.middleOf(pnt,this))
return pnt1;
else if (pnt2.middleOf(pnt,this))
return pnt2;
else{
if (dist<1)
return this;
else
return pnt;
}
}else{
if (pnt1.middleOf(pnt,this) || pnt.middleOf(pnt1,this))
return pnt1;
else
return pnt2;
}
}
/**
* Get a point on the line with specified slope, and with specified distance
* to this point, and also under same direction with specified reference point.
*
* @param slope A slope of specified line that accross this point.
*
* @param dist A distance from the wanted point to this point.
*
* @param refPoint A reference point.
*
* @param direction Direction that the vector from this point to wanted point according to the vector from this point to reference point.
*
* @return the point.
*
*/
public JFPoint nearPoint(double slope,double dist, JFPoint refPoint, boolean direction){
double x1=0, y1=0, x2=0, y2=0;
if (slope==GeomConst.LARGE_VALUE){
x1 =getX();
y1 =getY() + dist;
x2 =getX();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?