<< Chapter < Page Chapter >> Page >

Define two points as two locations in space

Listing 4 defines two points in space by instantiating two objects of the class named Point2D.Double , and saving references to those objects in variables of the superclass type Point2D .

Listing 4 . Define two points as two locations in space.
Point2D pointA = new Point2D.Double(-this.getWidth()/2.5,0.0);Point2D pointB = new Point2D.Double(this.getWidth()/2.5,0.0);

The left and right ends of the horizontal line

The object referred to as pointA represents the location in 2D space at the left end of the horizontal line shown in Figure 1 .

As explained by Kjell, the values used to represent this location in space are relativeto the current coordinate frame in which the origin is at the center of the canvas. Had the origin not been translated to the center of the canvas in Listing 3 , a different set of values would have been required to represent that same location in space.

Similarly, the object referred to as pointB represents the location in space at the right end of the horizontal line shown in Figure 1 .

Construct a line segment

Listing 5 uses the two points described above to construct an object that represents a line segment connecting the two points by instantiating an objectof the class named Line2D.Double .

As shown in Figure 1 , the values of the points relative to the current coordinate frame causes this line segment to behorizontal when the coordinate frame is displayed with the orientation shown in Figure 1 . On the other hand, a different rendering of the coordinate frame may have caused the line segment to be something other than horizontal.

Listing 5 . Construct a line segment.
Line2D.Double horizLine = new Line2D.Double(pointA,pointB);

Not the true line segment

The main point here is that the line segment created in Listing 5 represents a line segment between two points in space but it is not the true line segment. According to Kjell, the true line segment has no width, and therefore is not visibleto the human eye.

The line segment constructed in Listing 5 is simply a visual representation of thetrue line segment. When rendered on the screen, that line segment could take on an infinite number of appearances depending on how the space is rendered on thescreen. It just happens that in the case of Figure 1 , the rendering of that space on the screen causes the line segment to appear to be parallel to thebottom of a rectangular screen.

Construct an object that represents a vertical line segment

Listing 6 uses a similar procedure to construct an object that represents a line segment that is perpendicular to the previous line segment, and whichintersects the previous line segment at the origin of the current coordinate frame.

Listing 6 . Construct an object that represents a vertical line segment.
Point2D pointC = new Point2D.Double(0.0,-this.getHeight()/2.5);Point2D pointD = new Point2D.Double(0.0,this.getHeight()/2.5);Line2D.Double vertLine = new Line2D.Double(pointC,pointD);

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask