package org.csstudio.sds.components.ui.internal.figureparts;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

public class PolarPoint {
	
	/**
	 * The radial coordinate 
	 */
	public int r;
	
	
	/**
	 * The angular coordinate in radians
	 */
	public double theta;	
	
	/**
	 * @param r The radial coordinate 
	 * @param theta The angular coordinate in radians
	 */
	public PolarPoint(int r, double theta) {
		this.r = r;
		this.theta = theta;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof PolarPoint) {
			PolarPoint p = (PolarPoint)obj;
			return p.r == r && p.theta == theta;
		}
		return false;
	}
	
	/**
	 * Transform the polar point to the {@link Point} in rectangular coordinates. 
	 * The rectangular coordinates has the same origin as the polar coordinates.
	 * @return the point in rectangular coordinates
	 */
	public Point toPoint() {
		int x = (int) (r * Math.cos(theta));
		int y = (int) (-r * Math.sin(theta));
		return new Point(x, y);		
	}	
	
	/**
	 * Transform the polar point to the {@link Point} in the absolute coordinate system.
	 * It is assumed that the origin of the polar coordinate system is the central point of 
	 * the rectangle. 
	 * @param rect the paint area of the figure 
	 * @return the point in absolute coordinate system.
	 */
	public Point toAbsolutePoint(Rectangle rect) {
		Point p = this.toPoint();
		return p.translate(rect.width/2, rect.height/2).translate(
				rect.x, rect.y);
	}
	
	/**Transform the polar point to the {@link Point} in the relative coordinate system, 
	 * whose origin is (rect.x, rect.y). 
	 * It is assumed that the origin of the polar coordinate system is the central point of 
	 * the rectangle. 
	 * @param rect the paint area of the figure
	 * @return the point in relative coordinate system.
	 */
	public Point toRelativePoint(Rectangle rect) {
		Point p = this.toPoint();
		return p.translate(rect.width/2, rect.height/2);		
	}
	
	
}

@@ -144,16 +144,110 @@
 angle;%0A%0A
+/**%0A * A polar point in a standard polar coordinates system.%0A * %0A * @author Xihui Chen%0A *%0A */%0A
 public c
@@ -2056,10 +2056,1034 @@
 %0A%09%7D%0A%09%0A%09%0A
+%09/**%0A%09 * convert a point to polar point%0A%09 * @param pole the pole of the polar coordinate system.%0A%09 * @param point the point to be converted%0A%09 * @return the corresponding polar point.%0A%09 */%0A%09public static PolarPoint point2PolarPoint(Point pole, Point point) %7B%0A%09%09int x = point.x - pole.x;%0A%09%09int y = point.y - pole.y;%0A%09%09%0A%09%09double r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));%0A%09%09%0A%09%09double theta = Math.acos((double)x/r);%0A%09%09if(y %3E0)%0A%09%09%09theta = 2*Math.PI - theta;%0A%09%09return new PolarPoint((int) r, theta);%0A%09%7D%0A%09%0A%09/**rotate the x axis of the polar coordinate system to the axisDirection%0A%09 * @param axisDirection the direction of the new axis%0A%09 * @param inRadians true if the axisDirection is in radians, false if in degrees.%0A%09 */%0A%09public void rotateAxis(double axisDirection, boolean inRadians) %7B%0A%09%09if(!inRadians)%0A%09%09%09axisDirection = axisDirection * Math.PI/180.0;%0A%09%09theta -= axisDirection;%0A%09%09if(theta %3C 0) %0A%09%09%09theta += 2*Math.PI;%0A%09%7D%0A%09%0A%09@Override%0A%09public String toString() %7B%0A%09%09return %22(%22 + r + %22, %22 + theta * 180.0/Math.PI + %22)%22;%0A%09%7D%0A
 %7D%0A

add convert point to polar point