Sindbad~EG File Manager

Current Path : /usr/home/beeson/public_html/dynamicgeometry/org/dynamicgeometry/diagrammer/
Upload File :
Current File : /usr/home/beeson/public_html/dynamicgeometry/org/dynamicgeometry/diagrammer/ClosedPolygon.java

/**
 * This class represents a Closed Polygon displayed by Diagrammer.
 * File: ClosedPolygon.java
 *
 * This code is in the public domain.
 *
 * @author Brian Chan
 */
// 12.24.06  Beeson changed color to GREEN

package org.dynamicgeometry.diagrammer;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Point2D;
import java.util.*;

public class ClosedPolygon extends GeometricObject {

	/**
	 * A constructor for a ClosedPolygon
	 *
	 */
	public ClosedPolygon()
	{
		// calls the constructor of Geometric Object
		super();
	}

	/**
	 * Draws a Closed Polygon using the Graphics2D parameter.
	 *
	 * @param g the Object used to draw a Closed Polygon.
	 */
	public void draw(Graphics2D g)
	{
		Point a,
		      b;

		// draws this Closed Polygon only if the Arguments field is set correctly
		//   and is visible
		if(this.Arguments != null &&
		   this.Arguments.size() > 2 &&
		   this.visible)
		{
			// if this Object is input
			if(this.isInput != 0)
			{
				// if this Object is selected and is input
				if(this.selected)
					g.setColor(Color.GREEN);

				// if this Object is not selected but is input
				else
					g.setColor(Color.BLUE);
			}

			// if this Object is not input
			else
			{
				// if this Object is selected and is input
				if(this.selected)
					g.setColor(Color.RED);

				// if this Object is not selected but is input
				else
					g.setColor(Color.BLACK);
			}

			a = (Point)this.Arguments.get(0);
			b = (Point)this.Arguments.get(1);

			// draws the Label for this Segment if it is set and visible
			if(this.label != null && this.label.getVisible())
			{
				FontRenderContext frc = g.getFontRenderContext();

				TextLayout layout = new TextLayout(this.label.toString(),
						                           this.label.getFont(),
						                           frc);

				this.label.setLocation(
						new Point2D.Double((a.x + b.x) / 2 + EPSILON,
								           (a.y + b.y) / 2 + EPSILON));

				layout.draw(g,
						    (float)this.label.getLocation().getX(),
						    (float)this.label.getLocation().getY() );
			}

			// set the line thickness
			g.setStroke( new BasicStroke(lineThickness) );

			// draws the segments that make up a ClosedPolygon
			for(int i = 0; i < this.Arguments.size(); i++)
			{
				a = (Point)this.Arguments.get(i);
				b = (Point)this.Arguments.get( (i + 1) % this.Arguments.size());

				g.drawLine((int)a.x,
						   (int)a.y,
						   (int)b.x,
		  				   (int)b.y);
			}
		}
	}

	/**
	 * Checks if this Closed Polygon was clicked on by the user.
	 *
	 * Code used from:
	 * http://local.wasp.uwa.edu.au/~pbourke/geometry/sphereline/raysphere.c
	 *
	 * @param x the x coordinate of the mouse click
	 * @param y the y coordinate of the mouse click
	 * @return true if the mouse click was "close" to one of the edges of the
	 *              Closed Polygon, false if not.
	 */
	public boolean clickedOn(double x, double y)
	{
		// checks if there are valid Arguments for this ClosedPolygon and is visible
		if(this.Arguments != null &&
		   this.Arguments.size() > 2 &&
		   this.visible)
		{
			Point p1,
	              p2,
	              sc,
	              dp;

			// checks each individual segment that makes up the Closed Polygon
			for(int i = 0; i < this.Arguments.size(); i++)
			{
				p1 = (Point)this.Arguments.get(i);
				p2 = (Point)this.Arguments.get( (i + 1) % this.Arguments.size());

				sc = new Point(x, y);
				dp = new Point();

				double r = EPSILON,
			           a,
			           b,
			           c,
			           mu1,
			           mu2,
			           bb4ac;

				dp.x = p2.x - p1.x;
				dp.y = p2.y - p1.y;
				a = dp.x * dp.x + dp.y * dp.y;
				b = 2 * (dp.x * (p1.x - sc.x) + dp.y * (p1.y - sc.y));
				c = sc.x * sc.x + sc.y * sc.y;
				c += p1.x * p1.x + p1.y * p1.y;
				c -= 2 * (sc.x * p1.x + sc.y * p1.y);
				c -= r * r;
				bb4ac = b * b - 4 * a * c;
				if (bb4ac >= 0)
				{
					mu1 = (-b + Math.sqrt(bb4ac)) / (2 * a);
					mu2 = (-b - Math.sqrt(bb4ac)) / (2 * a);

					// returns true if this segment was clicked on
					if( (mu1 > 0 && mu1 < 1) || (mu2 > 0 && mu2 < 1) )
						return true;
				}
			}
		}
		return false;
	}

	/**
	 * Returns the list of Points used by this Closed Polygon.
	 *
	 * @return the Arguments of this Geometric Object.
	 */
	public ArrayList<GeometricObject> getPoints()
	{
		return this.Arguments;
	}

	/**
	 * Sets the new Points of the Closed Polygon if it is valid.
	 * A valid set of Points would have 3 or more Point Objects and
	 * cannot contain any other type of Geometric Object.
	 *
	 * @param points an Array List of Points
	 * @return true if the Arguments field was updated, false if not
	 */
	public boolean setPoints(ArrayList<GeometricObject> points)
	{
		if(points != null)
		{
			// Checks all Objects of points to see if they are Points
			for(GeometricObject go: points)
			{
				if(!(go instanceof Point))
					return false;
			}
		}

		// sets the Arguments field of this Geometric Object.
		this.Arguments = points;
		return true;
	}
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists