Sindbad~EG File Manager
/**
* This class represents a Ray displayed by Diagrammer.
* File: Segment.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.*;
import java.awt.geom.Point2D;
public class Segment extends Line {
/**
* A constructor for Segment.
*
*/
public Segment()
{
// calls a constructor of Line
super();
}
/**
* A constructor for Segment.
*
* @param A one end Point of the Segment
* @param B another end Point of Segment
*/
public Segment(Point A, Point B)
{
// calls a constructor of Line
super(A,B);
}
/**
* Draws a Segment using the Graphics2D parameter.
*
* @param g the Object used to draw an Arc
*/
public void draw(Graphics2D g)
{
Point a,
b;
// Checks if the Arguments are valid to draw a Segment
// draws if this Segment is visible
if(this.visible &&
this.Arguments.size() == 2 &&
(a = (Point)Arguments.get(0)) instanceof Point &&
(b = (Point)Arguments.get(1)) instanceof Point)
{
// 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);
}
g.drawLine((int)a.x,
(int)a.y,
(int)b.x,
(int)b.y);
// 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() );
}
}
}
/**
* Determines if a mouse click is "close" enough to constitute a mouse
* click for this Object.
*
* 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 this mouse click
* @return true if this Object was clicked, false if this Object was not clicked
*/
public boolean clickedOn(double x, double y)
{
Point p1,
p2,
sc,
dp;
// checks if there are valid Arguments for this Line and is visible
if(this.visible &&
this.Arguments.size() == 2 &&
(p1 = (Point)Arguments.get(0)) instanceof Point &&
(p2 = (Point)Arguments.get(1)) instanceof Point)
{
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)
return(false);
mu1 = (-b + Math.sqrt(bb4ac)) / (2 * a);
mu2 = (-b - Math.sqrt(bb4ac)) / (2 * a);
return (mu1 > 0 && mu1 < 1) || (mu2 > 0 && mu2 < 1);
}
// otherwise, return false
else return false;
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists