jfvector.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 312 行
JAVA
312 行
/**
* $Id:JFVector.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfgraph.geom;
import com.jfimagine.jfgraph.geom.JFPoint;
/**
* JFVector class. A class used to represent a graphic vector.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class JFVector implements Cloneable {
/**
* vector under x coordinate.
*/
private double m_x =0;
/**
* vector under y coordinate.
*/
private double m_y =0;
/**
* Get x coordiate vector of this vector.
*
* @return The x coordiate vector.
*
*/
public double getX(){
return m_x;
}
/**
* Set x coordiate vector of this vector.
*
* @param val A new x coordiate vector.
*
*/
public void setX(double val){
m_x =val;
}
/**
* Get y coordiate vector of this vector
*
* @return The y coordiate vector.
*
*/
public double getY(){
return m_y;
}
/**
* Set y coordiate vector of this point.
*
* @param val A new y coordiate vector.
*
*/
public void setY(double val){
m_y =val;
}
/**
* Set value of current JFVector.
*
* @param val A new JFVector object.
*
*/
public void setValue(JFVector val){
setValue(val.getX(),val.getY());
}
/**
* Set value of current JFVector.
*
* @param x, y A new JFVector coordinates vector.
*
*/
public void setValue(double x, double y){
m_x =x;
m_y =y;
}
/**
* Constructor for JFVector.
* Default to 0 for x and y coordinates.
*
*/
public JFVector(){
m_x =0;
m_y =0;
}
/**
* Constructor for JFVector.
*
* @param x X coordiate.
*
* @param y Y coordiate.
*
*/
public JFVector(double x, double y){
setValue(x,y);
}
/**
* Constructor for JFVector.
*
* @param val A JFVector.
*
*/
public JFVector(JFVector val){
setValue(val);
}
/**
* get the vector from base point to target point.
*
* @param basePoint The base point
*
* @param targetPoint The target point.
*
* @return The vector.
*
*/
public static JFVector getVector(JFPoint basePoint,JFPoint targetPoint){
return getVector(basePoint.getX(),basePoint.getY(),targetPoint.getX(),targetPoint.getY());
}
/**
* get the vector from base point to target point.
*
* @param baseX, baseY The base point
*
* @param targetX, targetY The target point.
*
* @return The vector.
*
*/
public static JFVector getVector(double baseX,double baseY,double targetX,double targetY){
//to consider a clockwise quadrant system actually,
//we use a negative sign in y coordinate vector calculation.
return new JFVector(targetX-baseX,-1*(targetY-baseY));
}
/**
* get the dot product between this vector and specified vector.
* The equation of dot product=x1 * x2 + y1 * y2
*
* @param target A specified target vector.
*
* @return The dot product.
*
*/
public double getDotProduct(JFVector target){
return m_x * target.getX() + m_y * target.getY();
}
/**
* get the cross product between this vector and specified vector.
* The equation of cross product=x1 * y2 - y1 * x2
*
* @param target A specified target vector.
*
* @return The cross product.
*
*/
public double getCrossProduct(JFVector target){
return m_x * target.getY() - m_y * target.getX();
}
/**
* if the test line is under clockwise side of base line.
*
* @param testLine A test line segment.
*
* @param baseLine A base line segment.
*
* @return True if test line is under clockwise side of base line, False otherwise.
*
*/
public static boolean underClockwiseSide(LineSeg testLine, LineSeg baseLine){
if (testLine==null || baseLine==null)
return false;
return underClockwiseSide(testLine.getPoint1(),testLine.getPoint2(), baseLine.getPoint1(), baseLine.getPoint2());
}
/**
* if the test line is under clockwise side of base line.
*
* @param testStartPoint, testEndPoint A test line segment.
*
* @param baseStartPoint, baseEndPoint A base line segment.
*
* @return True if test line is under clockwise side of base line, False otherwise.
*
*/
public static boolean underClockwiseSide(JFPoint testStartPoint, JFPoint testEndPoint, JFPoint baseStartPoint, JFPoint baseEndPoint){
return underClockwiseSide( testStartPoint.getX(), testStartPoint.getY(),
testEndPoint.getX(), testEndPoint.getY(),
baseStartPoint.getX(), baseStartPoint.getY(),
baseEndPoint.getX(), baseEndPoint.getY());
}
/**
* if the test line is under clockwise side of base line.
*
* @param testStartPointX, testStartPointY The startPoint of a test line segment.
*
* @param testEndPointX, testEndPointY The endPoint of a test line segment.
*
* @param baseStartPointX, baseStartPointY The startPoint of a base line segment.
*
* @param baseEndPointX, baseEndPointY The endPoint of a base line segment.
*
* @return True if test line is under clockwise side of base line, False otherwise.
*
*/
public static boolean underClockwiseSide(double testStartPointX, double testStartPointY,
double testEndPointX, double testEndPointY,
double baseStartPointX, double baseStartPointY,
double baseEndPointX, double baseEndPointY){
JFVector vector1 =getVector(testStartPointX, testStartPointY, testEndPointX, testEndPointY);
JFVector vector2 =getVector(baseStartPointX, baseStartPointY, baseEndPointX, baseEndPointY);
return (vector1.getCrossProduct(vector2) >0);
}
/**
* Convert this object to String
*
* @return An string represents the content of the object
*
*/
public String toString(){
StringBuffer buf =new StringBuffer();
buf.append(";x="); buf.append(m_x);
buf.append(";y="); buf.append(m_y);
return buf.toString();
}
/**
* Creates a new object of the same class and with the same contents as this object.
*
* @return A clone of this instance.
*
*/
public Object clone() throws CloneNotSupportedException{
try{
return new JFVector(this);
}catch(Exception e){
throw new CloneNotSupportedException(e.getMessage());
}
}
/**
* Returns the hashcode for this Object.
*
* @return hash code for this Point2D.
*
*/
public int hashCode(){
long x =Double.doubleToLongBits(m_x);
long y =Double.doubleToLongBits(m_y);
return (int)(x ^ (x >>> 32)) ^ (int)(y ^ (y >>> 32));
}
/**
* Determines whether or not two objects are equal.
*
* @param obj an object to be compared with this object
*
* @return true if the object to be compared is an instance of Port and has the same values; false otherwise.
*
*/
public boolean equals(Object obj){
if (obj == this)
return true;
if (!(obj instanceof JFVector))
return false;
JFVector val =(JFVector)obj;
return (m_x==val.getX()) && (m_y==val.getY());
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?