📄 position.java
字号:
/**
* Stores the position of an object in the simulation.
*
* @author James M. Clarke
* @version 02/03/2007
*/
public class Position
{
private float x;
private float y;
/**
* Constructor for Position objects
* @param xcoord the x coordinate of the object being simulated
* @param ycoord the y coordinate of the object being simulated
*/
public Position(float xcoord, float ycoord)
{
x = xcoord;
y = ycoord;
}
/**
* Accessor method for the x coordinate
*
* @return x coordinate of this position
*/
public float getX()
{ return x; }
/**
* Accessor method for the y coordinate
*
* @return y coordinate of this position
*/
public float getY()
{ return y; }
/**
* Finds the angle between the line drawn between two points and the "y" axis.
*
* @param p1 the <code>Position</code> of the start of the line
* @param p2 the <code>Position</code> of the end of the line
* @return the angle in radians clockwise from the y-axis to the line
*/
public static float angleToNorth(Position p1, Position p2)
{
//Find the angle (clockwise) between North and the line starting at (xpos1,ypos1) and ending at (xpos2, ypos2)
double temp = Math.atan( ((double) (p2.getY()-p1.getY()) / ((p2.getX()-p1.getX()) ) ) );
if (p2.getX() < p1.getX()) { temp += (Math.PI); }
temp += Math.PI/2;
return Angle.normal( (float) temp);
}
/**
* Finds the distance between two points
*
* @param p1 the <code>Position</code> of one of the points
* @param p2 the <code>Position</code> of the other point
* @return the distance between the points
*/
public static float distance(Position p1, Position p2)
{
//Get the distance between the two points (xpos1, ypos1) and (xpos2, ypos2)
float xtemp = (float) Math.pow(p2.getX()-p1.getX(),2);
float ytemp = (float) Math.pow(p2.getY()-p1.getY(),2);
float temp = xtemp + ytemp;
temp = (float) Math.sqrt(temp);
return temp;
}
/**
* Calculates a new point that is at a particular distance and heading from a starting point
*
* @param pos the <code>Position</code> to start at
* @param angle the angle to travel, in radians clockwise from the y-axis
* @param distance the distance to travel
* @return the new <code>Position</code>
*/
public static Position atAngleDistance(Position pos, float angle, float distance)
{
//Get the x coordinate of a point that is "distance" in the direction "angle" from a point (xpos, ypos)
return new Position( (distance * ((float) Math.cos((double) angle + (Math.PI/2) ))) + pos.getX(), (distance * ((float) Math.sin((double) angle + (Math.PI/2)))) + pos.getY() );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -