[an error occurred while processing this directive]
Domain for sale!
Start Search Contents Index Links About

Geometry in 2D space

Point, Rectangle and more

2002 - Week 49 - Havard Rast Blok

This week we will look at some of the classes for geometry in the Java API, some of which includesPoint, Rectangle, Polygon.

To start, investigate the three basic integer based geometry classes found in java.awt; namely Point, Polygon and Rectangle. Construct an object of each, and use it to make the corresponding figure on your screen.


BasicTest.java

Now get ready to implement your own shape; a triangle. First, decide upon the internal coordinate representation of the three points. You might choose to use Points, a Polygon, both or some other class or structure. As constructors you might include integers, Points or a Polygon as well. Finally, include a method draw( Graphics g ) that will draw your Triangle using the specified Graphics object.


Triangle.java

To enhance your Triangle class, you may let it implement the Shape interface found in java.awt.

Hint: Remember, that the Polygon class already implements this interface.


Triangle.java

Finally, this week, let's do some arithmetics. When viewing the Point class as an Vector in 2D space, there are a few methods that could be added: Calculating the length of the Vector, normalizing it down to length 1, scaling the Vector, or adding another Vector (like translate).

Hint: Some of these operations require decimal precision, so you should consider switching to the Point2D.Double class of java.awt.geom, or implementing your own from scratch.


Vector.java



site: Håvard Rast Blok
mail:
updated: 16 July 2010