Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2007

Tip: Looking for answers? Try searching our database.

Find point perpendicular to line

Thread view: 
Jeff Higgins - 22 Jul 2007 18:23 GMT
Hi,
 My trigonometry and imagination are failing me now.
I hope some one can give me help.

I have two points:

Point2D.Double A = new Point2D.Double(100d, 100d);
Point2D.Double B = new Point2D.Double(200d, 200d);

which can be used to describe a line.

Path2D.Double lineAB = new Path2D.Double();
moveTo(A.x, A.y);
lineTo(B.x, B.y);

I would like to find a third point that is:

on a line perpendicular to lineAB,
and
double distanceC = 3d;
distant from A

double distanceB = Math.sqrt(Math.pow((B.x - A.x), 2) + Math.pow((B.y -
A.y), 2));

My ultimate goal is to build a rectangle:
Rectangle2D.Double hitTestBounds;
that encloses lineAB such that lineAB splits
hitTestBounds in half, not diagonally.

Thanks,
Jeff Higgins
Lew - 22 Jul 2007 18:40 GMT
> I would like to find a third point that is:
>
> on a line perpendicular to lineAB,
> and
> double distanceC = 3d;
> distant from A

Every point on the circumference of a circle of radius 3d centered at A
fulfills your requirements.

If you further constrain the point to be at distance 3d from both points A and
B, then you have exactly two points that fulfill the requirements, those at
the intersections of the two circles of radius 3d centered at A and B.

It is meaningless to describe a point as "perpendicular to a line", as your
subject line requests.  Every point in the plane is on a line perpendicular to
AB, as requested in your message body.

Signature

Lew

Jeff Higgins - 22 Jul 2007 19:13 GMT
>> I would like to find a third point that is:
>>
>> on a line perpendicular to lineAB,

and passes throught point A

>> and
>> double distanceC = 3d;
[quoted text clipped - 11 lines]
> your subject line requests.  Every point in the plane is on a line
> perpendicular to AB, as requested in your message body.
Joshua Cranmer - 22 Jul 2007 19:45 GMT
>>> I would like to find a third point that is:
>>>
[quoted text clipped - 5 lines]
>>> double distanceC = 3d;
>>> distant from A

I belive this is your criteria:
     C
     |  -- distance 3d.
B-----A

If the line AB is at an angle theta to the origin line, then theta =
arctan (Ay-By)/(Ax-Bx). The angle that line AC makes with the origin is
phi, which is theta + pi/2 in this case. Translating the origin to point
A, we have that Cx is r*cos phi and Cy is r*sin phi.

phi should also be equal to arctan (Bx-Ax)/(Ay-By), and reducing, we get
the Cx = 3d / sqrt (1+x^2) + Ax and Cy = 3d * x /sqrt(1+x^2) + Ay where x
is (Bx-Ax)/(Ay-By), except if Ay=By (i.e., a horizontal line), where Cx =
Ax and Cy = Ay+3d.

Note: only checked for slope AB = 0, 1, and infinity.
Jeff Higgins - 23 Jul 2007 01:41 GMT
>>>> I would like to find a third point that is:
>>>>
[quoted text clipped - 22 lines]
>
> Note: only checked for slope AB = 0, 1, and infinity.

Joshua,
 Thanks very much.
Appreciative,
JH

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class HitTestBoundsTest
{
 static class GraphicPanel extends JPanel
 {
   Point2D.Double getMyPoint_PerpendicularToALine(
       Point2D.Double source,
       Point2D.Double target,
       double width)
   {
     // TODO test for horizontal line
     double x = ((target.x - source.x) / (source.y - target.y));
     double cx = (width / 2) / Math.sqrt(1 + Math.pow(x, 2)) + source.x;
     double cy = (width / 2) * x / Math.sqrt(1 + Math.pow(x, 2)) +
source.y;
     Point2D.Double ret = new Point2D.Double(cx, cy);
     return ret;
   }

   public void paintComponent( Graphics g )
   {
     Graphics2D g2 = (Graphics2D)g;
     Path2D.Double line = new Path2D.Double();
     line.moveTo(source.x, source.y);
     line.lineTo(target.x, target.y);
     Point2D.Double C =
       getMyPoint_PerpendicularToALine(source, target, width);
     Point2D.Double D =
       getMyPoint_PerpendicularToALine(target, source, width);
     Path2D.Double CD = new Path2D.Double();
     Path2D.Double EF = new Path2D.Double();
     Path2D.Double CE = new Path2D.Double();
     Path2D.Double DF = new Path2D.Double();
     CD.moveTo(C.x, C.y);
     CD.lineTo(D.x, D.y);
     EF.moveTo(C.y, C.x);
     EF.lineTo(D.y, D.x);
     CE.moveTo(C.x, C.y);
     CE.lineTo(C.y, C.x);
     DF.moveTo(D.x, D.y);
     DF.lineTo(D.y, D.x);
     g2.draw(CD);
     g2.draw(EF);
     g2.draw(CE);
     g2.draw(DF);
     g2.setColor(Color.RED);
     g2.draw(line);
   }

   public GraphicPanel(){}

   Point2D.Double source =
     new Point2D.Double(100d, 100d);
   Point2D.Double target =
     new Point2D.Double(200d, 200d);
   double width = 3d;
 }

 JFrame frame;
 JPanel panel;

 HitTestBoundsTest()
 {
   frame = new JFrame("HitTestBoundsTest");
   panel = new GraphicPanel();
   frame.setSize(600, 480);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.add(panel);
   frame.setVisible(true);
 }

 public static void main(String[] args)
 {
   HitTestBoundsTest test = new HitTestBoundsTest();
 }
}
Mark Space - 22 Jul 2007 20:47 GMT
> It is meaningless to describe a point as "perpendicular to a line", as
> your subject line requests.  Every point in the plane is on a line
> perpendicular to AB, as requested in your message body.

Unless, I think, the three points are co-linear, including the case
where one or more points are the same.
Lew - 22 Jul 2007 21:56 GMT
Lew wrote:
>> It is meaningless to describe a point as "perpendicular to a line", as
>> your subject line requests.  Every point in the plane is on a line
>> perpendicular to AB, as requested in your message body.

> Unless, I think, the three points are co-linear, including the case
> where one or more points are the same.

A and B must be distinct points, of course, else they would not define a line.
  Every point on the plane, including the points A and B themselves and any
point on AB, is therefore located on some perpendicular to AB.

Signature

Lew

Mark Space - 22 Jul 2007 20:48 GMT
> My ultimate goal is to build a rectangle:
> Rectangle2D.Double hitTestBounds;
> that encloses lineAB such that lineAB splits
> hitTestBounds in half, not diagonally.

Just curious: you say line, but do you mean perhaps line segment?  Is
this line AB infinite, or does it have a finite length, starting at A
and ending a B?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.