|
The Physiologist's Friend Simulation API | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectch.unizh.ini.friend.graphics.AbstractTransformable
ch.unizh.ini.friend.graphics.ConvexPolygon
public class ConvexPolygon
This class implements the notion of a convex polygon. It ensures that the polygon it represents is convex, no three vertices are co-linear and the vertices are in counterclockwise order. The implementaion of the intersection algorithm is based on a note from J. O'Rourke et al., Computer Graphics and Image Processing 19, 384-391 (1982).
The points
are stored as linear array are x,y pairs of coordinates.
Field Summary | |
---|---|
int |
npoints
The number of vertices ( xpoints and ypoints
may have more elements than vertices present). |
float[] |
points
The coordinates of the vertices, as x,y pairs. |
Constructor Summary | |
---|---|
ConvexPolygon()
Initializes xpoints and ypoints to three elements. |
|
ConvexPolygon(float[] points,
int npoints)
Ensures that the represented polygon is convex, no three vertices are co-linear and the vertices are in counterclockwise order. |
|
ConvexPolygon(int n)
Initializes xpoints and ypoints to n elements. |
Method Summary | |
---|---|
void |
addPoint(float x,
float y)
Adds the given vertex at the end of the allready added vertices iff the resulting polygon is still convex, no three vertices co-linear and in counterclockwise order. |
protected void |
addPointOfIntersection(float x,
float y)
Adds a point to the polygon iff it isCompliant(x, y) . |
protected void |
addPointSilently(float x,
float y)
Adds the given vertex at the end of the allready added vertices. |
Transformable |
apply(AffineTransform at)
Applies the given transformation to the vertices of the polygon. |
float |
area()
Calculates the area of this polygon. |
Object |
clone()
Clones the polygon by allocating a new polygon and copying the array of coordinates. |
boolean |
contains(double x,
double y)
Determines whether the given point (x, y) is contained by this instance of ConvexPolygon . |
boolean |
contains(double x,
double y,
double w,
double h)
Determines whether the given rectangle is completely contained by this instance of ConvexPolygon . |
boolean |
contains(Point2D point2D)
Determines whether the given point is contained by this instance of ConvexPolygon . |
boolean |
contains(Rectangle2D rectangle2D)
Determines whether the given rectangle is completely contained by this instance of ConvexPolygon . |
protected boolean |
doIntersect(float x11,
float y11,
float x12,
float y12,
float x21,
float y21,
float x22,
float y22)
Determines whether the two given lines intersect. |
protected float |
dotProd(float x1,
float y1,
float x2,
float y2)
Computes the dot product of the two given vectors. |
Rectangle |
getBounds()
Computes a bounding box around this instance of ConvexPolygon . |
Rectangle2D |
getBounds2D()
Computes a bounding box around this instance of ConvexPolygon . |
Point2D |
getCenter()
Computes the center of this ConvexPolygon . |
static ConvexPolygon |
getNGonInstance(float x,
float y,
float r,
int n)
Creates an instance of ConvexPolygon initalizing it
as a regular n-gon with the given radius of the enclosing circle
and the given center. |
static ConvexPolygon |
getNGonInstance(float r,
int n)
Creates an instance of ConvexPolygon initalizing it
as a regular n-gon with the given radius of the enclosing circle
and the center at (0,0). |
static ConvexPolygon |
getNGonInstance(int n)
Creates an instance of ConvexPolygon initalizing it
as a regular n-gon with radius 1.0 of the enclosing circle and
the center at (0,0). |
PathIterator |
getPathIterator(AffineTransform at)
Returns a PathIterator for this polygon, applying the
transform if not null . |
PathIterator |
getPathIterator(AffineTransform at,
double flatness)
Returns a PathIterator for this polygon, applying the
transform if not null . |
static ConvexPolygon |
getRectangleInstance(float x,
float y,
float w,
float h)
Creates an instance of ConvexPolygon initalizing it
as a rectangle. |
protected void |
intersect(float x11,
float y11,
float x12,
float y12,
float x21,
float y21,
float x22,
float y22,
float[] coords)
Intersects the two given lines and stores the intersection point in the given array. |
Intersectable |
intersect(Intersectable other)
Intersects this convex polygon with n vertices with the given convex polygon with m vertices and returns the intersection which is again a convex polygon with at most n+m vertices. |
boolean |
intersects(double x,
double y,
double w,
double h)
Determines whether the given rectangle intersects with this polygon. |
boolean |
intersects(Rectangle2D rect)
Determines whether the given rectangle intersects with this polygon. |
boolean |
isCompliant()
Determines whether the represented polygon is convex, has no three co-linear vertices and the vertices are counterclockwise ordered. |
boolean |
isCompliant(float x,
float y)
Determines whether the polygon obtained by adding the given point (x,y) at the end would still be convex, have no three co-linear vertices and have the vertices in counterclockwise order. |
protected boolean |
isInHalfPlane(float x1,
float y1,
float x2,
float y2,
float x,
float y)
Determines whether (x,y) is on the left of the straight line defined by (x1,y1) and (x2, y2) (looking from the first to the second), including the line itself. |
protected boolean |
isInPolygon(float x,
float y,
ConvexPolygon P)
Determines whether a given point is in the given polygon. |
protected boolean |
isStrictlyInHalfPlane(float x1,
float y1,
float x2,
float y2,
float x,
float y)
Determines whether (x,y) is on the left of the straight line defined by (x1,y1) and (x2, y2) (looking from the first to the second), excluding the line itself. |
protected float |
pointInHalfPlane(float x1,
float y1,
float x2,
float y2,
float x,
float y)
Determines whether (x,y) is on the left, right of or on the straight line defined by (x1,y1) and (x2, y2) (looking from the first to the second), including the line itself. |
protected float |
signedArea(float x1,
float y1,
float x2,
float y2,
float x3,
float y3)
Calculates the twice the signed area of the defined by the given three vertices p1, p2 and p3, where the sign is positive iff (p1p2p3) form a counterclockwise cycle. |
String |
toString()
Returns a String -representation of the polygon. |
Methods inherited from class ch.unizh.ini.friend.graphics.AbstractTransformable |
---|
rotate, rotate, scale, scale, translate |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface ch.unizh.ini.friend.graphics.Transformable |
---|
rotate, rotate, scale, scale, translate |
Field Detail |
---|
public int npoints
xpoints
and ypoints
may have more elements than vertices present).
public float[] points
Constructor Detail |
---|
public ConvexPolygon()
xpoints
and ypoints
to three elements.
public ConvexPolygon(int n)
xpoints
and ypoints
to n elements.
n
- The number of vertices that initially can be stored with this polygon.public ConvexPolygon(float[] points, int npoints)
npoints
will be set to
two.
points
- The x,y-coordinates of the vertices.npoints
- The number of initial vertices.Method Detail |
---|
public static ConvexPolygon getNGonInstance(float x, float y, float r, int n)
ConvexPolygon
initalizing it
as a regular n-gon with the given radius of the enclosing circle
and the given center.
x
- x-coordinate of the center.y
- y-coordinate of the center.r
- The radius of the enclosing circle.n
- The number of edges the polygon will have.
public static ConvexPolygon getNGonInstance(float r, int n)
ConvexPolygon
initalizing it
as a regular n-gon with the given radius of the enclosing circle
and the center at (0,0).
r
- The radius of the enclosing circle.n
- The number of edges the polygon will have.
public static ConvexPolygon getNGonInstance(int n)
ConvexPolygon
initalizing it
as a regular n-gon with radius 1.0 of the enclosing circle and
the center at (0,0).
n
- The number of edges the polygon will have.
public static ConvexPolygon getRectangleInstance(float x, float y, float w, float h)
ConvexPolygon
initalizing it
as a rectangle.
x
- x-coordinate of the vertex with smallest value.y
- y-coordinate of the vertex with smallest value.w
- Width of the rectangle.h
- Height of the rectangle.
public boolean isCompliant()
protected float signedArea(float x1, float y1, float x2, float y2, float x3, float y3)
x1
- x-coordinate of the first vertex.y1
- y-coordinate of the first vertex.x2
- x-coordinate of the second vertex.y2
- y-coordinate of the second vertex.x3
- x-coordinate of the third vertex.y3
- y-coordinate of the third vertex.
public boolean isCompliant(float x, float y)
public void addPoint(float x, float y)
x
- x-coordinate of the vertex to add.y
- y-coordinate of the vertex to add.protected void addPointSilently(float x, float y)
npoints
will not be increased
and no checks regarding the conformance are made.
x
- x-coordinate of the vertex to add.y
- y-coordinate of the vertex to add.public boolean contains(Rectangle2D rectangle2D)
ConvexPolygon
.
contains
in interface Shape
rectangle2D
- The rectangle to check.
public boolean contains(Point2D point2D)
ConvexPolygon
.
contains
in interface Shape
point2D
- The point to check.
public boolean contains(double x, double y)
ConvexPolygon
.
contains
in interface Shape
x
- x-coordinate of the point to check.y
- y-coordinate of the point to check.
public boolean contains(double x, double y, double w, double h)
ConvexPolygon
.
contains
in interface Shape
x
- x-coordinate of the vertex with the smallest value.y
- y-coordinate of the vertex with the smallest value.w
- Width of the rectangle.h
- Height of the rectangle.
public Rectangle getBounds()
ConvexPolygon
.
getBounds
in interface Shape
Rectangle
.public Rectangle2D getBounds2D()
ConvexPolygon
.
getBounds2D
in interface Shape
Rectangle2D
.public Point2D getCenter()
ConvexPolygon
.
This is the average location of all the points the make up the polygon.
public PathIterator getPathIterator(AffineTransform at)
PathIterator
for this polygon, applying the
transform if not null
.
getPathIterator
in interface Shape
at
- The transform to apply.
PathIterator
PathIterator
public PathIterator getPathIterator(AffineTransform at, double flatness)
PathIterator
for this polygon, applying the
transform if not null
.
getPathIterator
in interface Shape
at
- The transform to apply.flatness
- Has no meaning because the polygon is flat by nature.
PathIterator
PathIterator
public Object clone()
clone
in interface Transformable
clone
in class AbstractTransformable
public boolean intersects(Rectangle2D rect)
intersects
in interface Shape
rect
- The rectangle to check against.
public boolean intersects(double x, double y, double w, double h)
intersects
in interface Shape
x
- x-coordinate of the vertex with the smallest value.y
- y-coordinate of the vertex with the smallest value.w
- width of the rectangle.h
- height of the rectangle.
protected float dotProd(float x1, float y1, float x2, float y2)
x1
- x-coordinate of the first vector.y1
- y-coordinate of the first vector.x2
- x-coordinate of the second vector.y2
- y-coordinate of the second vector.
protected float pointInHalfPlane(float x1, float y1, float x2, float y2, float x, float y)
x1
- x-coordinate of the first point defining the half-plane.y1
- y-coordinate of the first point defining the half-plane.x2
- x-coordinate of the second point defining the half-plane.y2
- y-coordinate of the second point defining the half-plane.x
- x-coordinate of the point to check.y
- y-coordinate of the point to check.
protected boolean isInHalfPlane(float x1, float y1, float x2, float y2, float x, float y)
x1
- x-coordinate of the first point defining the half-plane.y1
- y-coordinate of the first point defining the half-plane.x2
- x-coordinate of the second point defining the half-plane.y2
- y-coordinate of the second point defining the half-plane.x
- x-coordinate of the point to check.y
- y-coordinate of the point to check.
protected boolean isStrictlyInHalfPlane(float x1, float y1, float x2, float y2, float x, float y)
x1
- x-coordinate of the first point defining the half-plane.y1
- y-coordinate of the first point defining the half-plane.x2
- x-coordinate of the second point defining the half-plane.y2
- y-coordinate of the second point defining the half-plane.x
- x-coordinate of the point to check.y
- y-coordinate of the point to check.
protected boolean doIntersect(float x11, float y11, float x12, float y12, float x21, float y21, float x22, float y22)
x11
- x-coordinate of the first point of the first line.y11
- y-coordinate of the first point of the first line.x12
- x-coordinate of the second point of the first line.y12
- y-coordinate of the second point of the first line.x21
- x-coordinate of the first point of the second line.y21
- y-coordinate of the first point of the second line.x22
- x-coordinate of the second point of the second line.y22
- y-coordinate of the second point of the second line.
protected void intersect(float x11, float y11, float x12, float y12, float x21, float y21, float x22, float y22, float[] coords)
x11
- x-coordinate of the first point of the first line.y11
- y-coordinate of the first point of the first line.x12
- x-coordinate of the second point of the first line.y12
- y-coordinate of the second point of the first line.x21
- x-coordinate of the first point of the second line.y21
- y-coordinate of the first point of the second line.x22
- x-coordinate of the second point of the second line.y22
- y-coordinate of the second point of the second line.coords
- The array the intersection point is stored in.protected void addPointOfIntersection(float x, float y)
isCompliant(x, y)
.
protected boolean isInPolygon(float x, float y, ConvexPolygon P)
x
- x-coordinate of the point.y
- y-coordinate of the point.P
- the polygon to check against.
public Intersectable intersect(Intersectable other)
intersect
in interface Intersectable
Q
- The polygon to intersect with.
public float area()
area
in interface Intersectable
public String toString()
String
-representation of the polygon.
toString
in class Object
public Transformable apply(AffineTransform at)
apply
in interface Transformable
at
- The affine transformation to apply.
|
http://www.ini.unizh.ch/~tobi/friend | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |