The ClosestPointTo returns the closest point on the calling Segment to the argument position. The closest point may be anywhere on the segment including the endpoints.
The following creates a Segment and a Line as a visible representation of the Segment. The update method finds the closest point on the Segment to the Mouse and positions a Sprite there.
Add the following using statements:
Add the following at class scope:
Add the following to Initialize after initializing FlatRedBall:
Add the following to Update:
A segment is a object defined by two Points. It is not a PositionedObject and its points are absolute, which differentiates it from the Line class.
The following code creates two Lines. One line rotates automatically while the other is controlled by input from the Keyboard. The two Lines call the AsSegment method to create Segments that represent the calling Lines. The segment can then be used to find the point of collision between the two Lines. If the resulting intersection point has valid coordinates then a collision has occurred. A red ball Sprite marks the intersection point between the two Lines.
Add the following using statements:
Add the following at class scope:
Add the following in Initialize after initializing FlatRedBall:
Add the following in Update:
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.