Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Point
- {
- private final double DEFAULT_COORDINATE=90;
- private final double DEFAULT_VAL=0;
- private double _radius;
- private double _alpha;
- /**
- * Constructor for objects of class Point. Construct a new point with the specified x y coordinates. If the x coordinate is negative it is set to zero. If the y coordinate is negative it is set to zero.
- *@param
- *x - The x coordinate
- *@param
- *y - The y coordinate*/
- public Point (double x, double y)
- {
- if(x>=DEFAULT_VAL)
- x=x;
- else
- x=DEFAULT_VAL;
- if(y>=DEFAULT_VAL)
- y=y;
- else
- y=DEFAULT_VAL;
- if (x==DEFAULT_VAL)
- _alpha=DEFAULT_COORDINATE;
- else
- _alpha=getAlpha(x,y);
- _radius=getRadius(x,y);
- }
- private double getRadius (double x,double y)
- {
- _radius=Math.sqrt((Math.pow(x,2)+Math.pow(y,2)));
- return _radius;
- }
- private double getAlpha (double x, double y)
- {
- _alpha=Math.atan(y/x);
- _alpha =(180/Math.PI)*_alpha;
- return _alpha;
- }
- /**
- *Constructor for objects of class Point. Copy constructor, construct a point using another point.
- *@param
- *other - The point from which to construct the new object
- */
- public Point (Point other)
- {
- _radius=other._radius;
- _alpha=other._alpha;
- }
- /**This method returns the x coordinate of the point.
- *@return
- *The x coordinate of the point*/
- public double getX()
- {
- double returnredian=(_alpha)*(Math.PI/180);
- double returnX= Math.cos(returnredian)*_radius;
- double x=Math.round(returnX*10000)/(double)10000;
- return x;
- }
- /**This method returns the y coordinate of the point.
- *@return
- *The y coordinate of the point*/
- public double getY()
- {
- double returnredian=_alpha/(180/Math.PI);
- double returnY= Math.sin(returnredian)*_radius;
- double y=Math.round(returnY*10000)/(double)10000;
- return y;
- }
- /**This method sets the x coordinate of the point. If the new x coordinate is negative the old x coordinate will remain unchanged.
- *@param
- *x - The new x coordinate
- */
- public void setX (double x)
- {
- double currentY = getY();
- if(x>=DEFAULT_VAL)
- x=x;
- else
- x=DEFAULT_VAL;
- _radius=getRadius (x,currentY);
- _alpha=getAlpha (x,currentY);
- _alpha=Math.round(_alpha*10000)/(double)10000;
- }
- /**This method sets the y coordinate of the point. If the new y coordinate is negative the old y coordinate will remain unchanged.
- *@param
- *y - The new y
- */
- public void setY (double y)
- {
- double currentX = getX();
- if(y>=DEFAULT_VAL)
- y=y;
- else
- y=DEFAULT_VAL;
- _radius=getRadius (currentX,y);
- _alpha=getAlpha (currentX,y);
- _alpha=Math.round(_alpha*10000)/(double)10000;
- }
- /**Returns a string representation of Point in the format (x,y).
- *@Overrides
- *toString in class java.lang.Object
- *@return
- *A String representation of the Point*/
- public String toString()
- {
- return "("+getX()+","+getY()+")";
- }
- /**Check if the given point is equal to this point.
- *@param
- *other - The point to check equality with
- *@return
- *True if the given point is equal to this point
- */
- public boolean equals (Point other)
- {
- return getX() == other.getX() && getY() == other.getY();
- }
- /**Check if this point is above a received point.
- *@param
- *other- The point to check if this point is above
- *@return
- *True if this point is above the other point*/
- public boolean isAbove (Point other)
- {
- return other.getY()<getY();
- }
- /**Check if this point is below a received point.
- *@param
- *other - The point to check if this point is below
- *@return
- *True if this point is below the other point*/
- public boolean isUnder (Point other)
- {
- return other.getY()>getY();
- }
- /**Check if this point is left or a received point.
- *@param
- *other- The point to check if this point is left of.
- *@return
- *True if this point is left of the other point.*/
- public boolean isLeft (Point other)
- {
- return other.getX()>getX();
- }
- /**Check if this point is right of a received point.
- *@param
- *other - The point to check if this point is right of
- *@return
- *True if this point is right of the other point*/
- public boolean isRight (Point other)
- {
- return other.getX()<getX();
- }
- /**Check the distance between this point and a given point.
- *@param
- *other - The point to check the distance from
- *@return
- *the distance*/
- public double distance (Point other)
- {
- return Math.sqrt(Math.pow((other.getY()-getY()),2)+Math.pow((other.getX()-getX()),2));
- }
- /**Moves a point. If either coordinate becomes negative the point remains unchanged.
- *@param
- *dx - The difference to add to x
- *@param
- *dy - The difference to add to y*/
- public void move (double dX, double dY)
- {
- if((getX()+dX)>=DEFAULT_VAL&&(getY()+dY)>=DEFAULT_VAL)
- {
- setX(getX()+dX);
- setY(getY()+dY);}
- }
- }
- public class Segment1
- {
- private Point _poLeft;
- private Point _poRight;
- final double DEFAULT_VAL=0;
- /**Constructs a new segment using two Points. If the y coordinates are different, change the y of the right point to be equal to the y of the left point.
- *@param
- *@param left - the left point of the segment
- *@param right - the right point of the segment*/
- public Segment1 (Point left, Point right)
- {
- _poLeft=new Point(left);
- _poRight=new Point(right);
- double leftY=_poLeft.getY();
- double rightY = _poRight.getY();
- if (leftY!=rightY)
- _poRight.setY(leftY);
- }
- /**Constructs a new segment using 4 specified x y coordinates: Two coordinates for the left point and two coordinates for the right point. If the y coordinates are different, change the y of the right point to be equal to the y of the left point.
- *@param
- *@param leftx-X value of left point.
- *@param lefty-Y value of left point.
- *@param rightx-X value of right point.
- *@param righty-Y value of right point.
- */
- public Segment1 (double leftx, double lefty ,double rightx, double righty)
- {
- if (lefty!=leftx)
- righty=lefty;
- _poLeft = new Point(leftx, lefty);
- _poRight = new Point(leftx, lefty);
- }
- /**Copy Constructor. Construct a segment using a reference segment
- @param
- *other - the reference segment*/
- public Segment1 (Segment1 other)
- {
- _poLeft=new Point (other._poLeft);
- _poRight=new Point (other._poRight);
- }
- /**Returns the left point of the segment.
- *@return
- *The left point of the segment*/
- public Point getPoLeft()
- {
- return new Point(_poLeft);
- }
- /**Returns the right point of the segment.
- *@return
- *The right point of the segment*/
- public Point getPoRight() {
- return new Point (_poRight);
- }
- /**Returns the segment length.
- *@return
- *The segment length*/
- public double getLength(){
- return _poLeft.distance(_poRight);
- }
- /**Return a string representation of this segment in the format (3.0,4.0)---(3.0,6.0).
- *@override
- *toString in class java.lang.Object
- *@return
- *String representation of this segment*/
- public String toString()
- {
- return _poLeft.toString()+"---"+_poRight.toString();
- }
- /**Check if the reference segment is equal to this segment.
- *@param
- *other - the reference segment
- *@return
- *True if the reference segment is equal to this segment*/
- public boolean equals (Segment1 other)
- {
- return _poLeft==other._poLeft&&_poRight==other._poRight;
- }
- /**Check if this segment is above a reference segment.
- *@param
- *other - the reference segment
- *@return
- *True if this segment is above the reference segment*/
- public boolean isAbove (Segment1 other)
- {
- return _poLeft.isAbove(other._poLeft);
- }
- /**Check if this segment is under a reference segment.
- *@param
- *other - the reference segment
- *@return
- *True if this segment is under the reference segment*/
- public boolean isUnder (Segment1 other)
- {
- return other._poLeft.isAbove(_poLeft);
- }
- /**Check if this segment is left of a received segment.
- *@param
- *other - the reference segment
- *@return
- *True if this segment is left to the reference segment*/
- public boolean isLeft (Segment1 other)
- {
- return _poLeft.isLeft(other._poLeft)&&_poRight.isLeft(other._poRight);
- }
- /**Check if this segment is right of a received segment.
- *@param
- *other - the reference segment
- *@return
- *True if this segment is right to the reference segment*/
- boolean isRight (Segment1 other)
- {
- return _poLeft.isRight(other._poLeft)&&_poRight.isRight(other._poRight);
- }
- /**Move the segment horizontally by delta.
- *@param
- *delta - the displacement size*/
- public void moveHorizontal (double delta)
- {
- _poLeft.move(delta, 0);
- _poRight.move(delta, 0);
- }
- /**Move the segment vertically by delta.
- *@param
- *delta - the displacement size*/
- public void moveVertical (double delta)
- {
- _poLeft.move(0,delta);
- _poRight.move(0,delta);
- }
- /**Change the segment size by moving the right point by delta. Will be implemented only for a valid delta: only if the new right point remains the right point.
- *@param
- *delta - the length change*/
- public void changeSize (double delta)
- {
- if (_poLeft.isLeft(_poRight)){
- _poRight.move(_poRight.getX()+delta,_poRight.getY());
- }
- }
- /**Check if a point is located on the segment.
- *@param
- *p - a point to be checked
- *@return
- *True if p is on this segment*/
- public boolean pointOnSegment (Point p)
- {
- return p.getY()==_poLeft.getY() &&
- _poLeft.getX()<=p.getX() &&p.getX()>=_poRight.getX();
- }
- /**Check if this segment is bigger than a reference segment.
- *@param
- *other - the reference segment
- *@return
- *True if this segment is bigger than the reference segment*/
- public boolean isBigger (Segment1 other)
- {
- return getLength()>other.getLength();
- }
- /**Returns the overlap size of this segment and a reference segment.
- *@param
- *other - the reference segment
- *@return
- *The overlap size*/
- public double overlap (Segment1 other)
- {
- double Ls1=_poLeft.distance(_poRight);
- double Ls2=other._poLeft.distance(other._poRight);
- //option #1
- if (_poLeft.getX()>=other._poLeft.getX()&&_poRight.getX()>other._poLeft.getX()&&_poLeft.getX()<other._poRight.getX()&&_poRight.getX()<=other._poRight.getX())
- return Ls1;
- //option #2
- else if (other._poRight.getX()<_poLeft.getX()&&other._poRight.getX()<_poRight.getX()&&other._poRight.isRight(_poLeft))
- return DEFAULT_VAL;
- //option #3
- else if (other._poLeft.getX()>=_poLeft.getX()&&other._poRight.getX()<=_poRight.getX()&&other._poLeft.getX()<_poRight.getX()&&other._poRight.getX()>_poLeft.getX())
- return Ls2;
- //option #4
- else if (_poLeft.getX()<other._poLeft.getX()&&_poRight.getX()>other._poLeft.getX()&&_poRight.getX()<other._poRight.getX())
- return _poRight.getX()-other._poLeft.getX();
- //option #5
- else if (_poRight.getX()<other._poLeft.getX()&&_poRight.getX()<other._poRight.getX()&&_poRight.isRight(other._poLeft))
- return DEFAULT_VAL;
- //option #6
- else if (other._poLeft.getX()<_poLeft.getX()&&_poLeft.getX()<other._poRight.getX()&&_poRight.getX()>other._poRight.getX())
- return other._poRight.getX()-_poLeft.getX();
- else return DEFAULT_VAL;
- }
- /**Compute the trapeze perimeter, which is constructed by this segment and a reference segment.
- *@param
- *other - the reference segment
- *@return
- *The trapeze perimeter*/
- public double trapezePerimeter (Segment1 other)
- {
- double L1=other._poLeft.distance(other._poRight);
- double L2=_poLeft.distance(_poRight);
- double L3=_poLeft.distance(other._poLeft);
- double L4=_poRight.distance(other._poRight);
- return L1+L2+L3+L4 ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement