views:

94

answers:

6

I want to find out the X and Y position of a point which is halfway between a point and another point, in VB.net. Since there is no "direction" property (which would make this much easier...), I have no idea how to do this.

C# code is acceptable as well, though I would prefer vb as it wouldn't require a port.

Thanks for the help, this is bugging me like crazy!

+4  A: 

Remember your math?

x = (x0 + x1) / 2
y = (y0 + y1) / 2

That will give you the coordinates of the point halfway in between (x0, y0) and (x1, y1).

Anthony Mills
Oh man I overcomplicated this, I was trying trig >.>
Cyclone
A: 

If point one (x1, y1) and point two is (x2, y2), then the midpoint is (0.5*(x1 + x2), 0.5*(y1 + y2)).

Stephen Canon
A: 

This is actually quite easy.

PointF p1 = new PointF(..., ...);
PointF p2 = new PointF(..., ...);

PointF midPoint = new PointF((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);

The midpoint of two points (x1, y1) and (x2, y2) is the points ((x1 + x2) / 2, (y1 + y2) / 2).

Daniel Brückner
A: 
Point A=new Point(1,1);
Point B=new Point(6,8);
Point C=new Point(); //between point
C.X = (A.x+B.x)/2;
C.Y = (A.y+B.Y)/2;

Of course, if you were using polar coordinates by any chance, you would need a different formula:

d = sqrt(r1**2+r2**2-2(r1)(r2)cos[theta2-theta1]).

** X means "to the X power" :D

CrazyJugglerDrummer
+1  A: 

You should be able to just take the average of the two points:

Dim point1 as New Point(5, 2)
Dim point2 as New Point(25, 32)

Dim middlepoint as New Point((point1.X + point2.X)/2, (point1.Y + point2.Y)/2)

If you're trying to move from one point to a second, you'll probably want something more like:

Public Function MoveBetweenPoints(Point point1, Point point2, Double percentage) As Point
    Dim x as Double
    Dim y as Double

    x = point1.X * (1.0-percentage) + point2.X * percentage
    y = point1.Y * (1.0-percentage) + point2.Y * percentage

    Return New Point(x,y)
End Function

This would let you move from point1 to point2, by specifying the percentage to move (from 0-1)

Reed Copsey
A: 

A simple linear interpolation will do the trick.

R = P1 + (P2 - P1) * f;

R being the result position, P1 the first position, P2 the second position and f the interpolation factor (0.5 for the exact middle).

For a point, just apply on both coordinate if your point class doesn't support basic maths (such as System.Drawing.Point)

Dim R as Point;
R.X = P1.X + (P2.X - P1.X) * f;
R.Y = P1.Y + (P2.Y - P1.Y) * f;
Coincoin