当前位置: 动力学知识库 > 问答 > 编程问答 >

math - How to get the endpoint of a line in Java given the midpoint and the other endpoint

问题描述:

I know how to do the maths on paper but I am struggling to work out how to take potential negative values into account.

Given 2 locations: endpoint(x1, y1) and midpoint(x2, y2)

Find the other endpoint location

网友答案:
Point2D endpoint; //defined elsewhere
Point2D midpoint; //defined elsewhere

Point2D otherend = new Point2D(endpoint.getX() + 2 * (midpoint.getX() - endpoint.getX()),
                               endpoint.getY() + 2 * (midpoint.getY() - endpoint.getY()));

(Sorry -- I made a bonheheaded mistake the first time.)

网友答案:

I would comment, but my rep is too low. Without code it would be hard to tell. What you need to do is distinguish absolute distance between the two, determine direction based on whether the distance between the two is negative or positive, and do the math on that.

Example: x1 = 2, x2 = 0, y1 = 0, y2 = 0
   distance = -2x, 0y
   x2 - 2 = -2 = x3
   y3 = 0

Easier if you show us some code, but in other words just find the distance between x1 and x2, make that the distance between x2 and x3. Do the same for y's.

网友答案:

You could use these two points to find the equation of the line(y=mx+b) and by finding the length of the line portion you know(Sqrt(x^2+y^2)) then draw the line in the positive x direction for the length you have found.

分享给朋友:
您可能感兴趣的文章:
随机阅读: