Getting all points in vector (between two points) - objective-c

Lets say I have 2 points (x1,y1) and (x2,y2). And I can draw vector from point (x1,y1) to point (x2,y2). How can I get all possible points between them at for example every 10 pixels?
Simple visualization:

The vector between point A and a point B is B-A (x2-x1, y2-y1)
If you normalize that vector, and multiply it by the factor you want (it seems you want a distance of 10px, so your factor is 10), you can get all the points by adding it to the a current point (which initially is the origin A) until you reach the end point B.

You can take a smaller stepVector and add him step by step.
PseudoCode:
stepVector = yourVector / 10
Point1 = basePoint + stepVector
Point2 = Point1 + stepVector
...
or something line
stepVector = yourVector / 10
Point1 = basePoint + stepVector
Point2 = basePoint + (stepVector * 2)
Point3 = basePoint + (stepVector * 3)
...

Related

Obtaining a quadrilateral from four points then splitting it into two triangles

Given 4 unordered points, how do I obtain two triangles from those points WITHOUT forming an hourglass shape or having the triangles overlap. Convex quadrilaterals are fine, but I'd prefer a method that would remove the point near the center bounded by the other points within a single triangle. I have a semi-working solution, but it isn't pretty. I have previously tried Delaunay triangulation, forming 4 triangles via a center point and moving around it radially adding points to create triangles, amongst other methods. I cannot seem to find any information of this topic besides splitting triangles.
So this is what I did and it seemed to work well. For the first triangle, take the first 3 points and make that a triangle. Then, store a list of the midsections of the points for each edge. The midsection that is closest to the fourth point is on the edge that has the other 2 points that will make your second triangle.
pseudo code
func getMidsection(Point a, Point b) -> Point
{
Point midsection = Point(
(a.x + b.x) / 2,
(a.y + b.y) / 2,
(a.z + b.z) / 2
);
return midsection;
}
func getTrianglesFromPoints(Point[4] points) -> Triangle[2]
{
// define first triangle as first 3 points
Triangle tri1 = Triangle(points[0], points[1], points[2]);
Point[3] midsections;
float recordDist = -1;
int closestMidsection;
// loop through each edge in the first triangle
for(i : [0, 3) )
{
// get the midsection using the current point and next point in the first triangle
midsections[i] = getMidsection(points[i], points[(i+1)%3]);
// if the 4th point's distance to the midsection is smaller than past values, set the smallest dist to that point
if(dist(points[3], midsections[i]) < recordDist or recordDist == -1)
{
recordDist = dist(points[3], midsections[i]);
closestMidsection = i;
}
}
// define triangle2 from the closest midsection
Triangle tri2 = Triangle(points[closestMidsection], points[(closestMidsection + 1) % 3, points[3]);
// return the triangles
return [tri1, tri2];
}

How to teleport a player two block on his left?

I tried several things, like using vectors, but It didn't work for me. Than I tried searching on the internet and it didn't work as well.
Vector direc = l.getDirection().normalize();
direc.setY(l.getY());
direc.normalize();
direc.multiply(-1);
l.add(direc);
Player#teleport(l.getBlock().getLocation());
// or
Player#teleport(l);
Use Vector#rotateAroundY​ to rotate the player's direction vector 90 degrees to the left.
Vector dir = player.getLocation().getDirection(); // get player's direction vector
dir.setY(0).normalize(); // get rid of the y component
dir.rotateAroundY(Math.PI / 2); // rotate it 90 degrees to the left
dir.multiply(2); // make the vector's length 2
Location newLocation = player.getLocation().add(dir); // add the vector to the player's location to get the new location
Location location = player.getLocation();
Vector direction = location.getDirection();
direction.normalize();
float newZ = (float)(location.getZ() + (2 * Math.sin(Math.toRadians(location.getYaw() + 90 * direction)))); //2 is your block amount in Z direction
float newX = (float)(location.getX() + (Math.cos(Math.toRadians(location.getYaw() + 90 * direction))));
You have to know in which direction you want to teleport the player

Drawing triangles on a slope

I am writing an objective-c method that draws a series of triangles on a slope. In order to complete this, I need to calculate the vertex point of each triangle (C,D). The position starting and ending points are variable.
This seems like it should be an easy math problem. But so far I haven't been able to work it out on paper. Can anyone point me in the right direction?
No trigonometry involved.
Let D= Sqrt(X12^2+Y12^2) the Euclidean distance between P1 and P2 (X12 = X2-X1 and Y12 = Y2-Y1), and let p= P/D, a= A/D.
If P1P2 was the line segment (0, 0)-(1, 0), the vertices would be at (0, 0), (a, p/2), (0, p), (a, 3p/2), (0, 2p)...
The transform below scales and rotates (0, 0)-(1, 0) to P1P2:
X = X1 + X12.x - Y12.y
Y = Y1 + Y12.x + X12.y
Set triangle at origin horizontally:
(0, 0), (p, 0), (p/2, a)
Rotate to get needed slope alpha:
(0, 0), (p*cos(alpha), p*sin(alpha)), (p/2 * cos(alpha) - a * sin(alpha), p/2 * sin(alpha) + a*sin(alpha))
Shift by adding (x1, y1) to all of the coordinates.
The third coordinate is your vertex:
(Cx, Cy) = (p/2 * cos(alpha) - a * sin(alpha) + x1, p/2 * sin(alpha) + a*sin(alpha) + y1)
To find other vertices use the fact that they are shifted by p from each other, under the angle alpha:
(Cx_i, Cy_i) = (Cx, Cy) + i*(p * cos(alpha), p * sin(alpha))

How can I find all points between point1 and point 2 - Objective c? [duplicate]

I've got two points between which im drawing a line (x1,y1 and x2,y2) but i need to know the coordinates of x3,y3 which is gapSize away from point x2,y2. Any ideas on how to solve this problem (the program is written in objective-c if that is helpful at all)?
You can simply calculate the angle in radians as
double rads = atan2(y2 - y1, x2 - x1);
Then you get the coordinates as follows:
double x3 = x2 + gapSize * cos(rads);
double y3 = y2 + gapSize * sin(rads);
Is this what you meant?
Compute the distance between P1 and P2: d=sqrt( (y2-y1)^2 + (x2-x1)^2)
Then x2 = (d*x1 + gapSize*x3) / (d+gapSize)
So x3 = (x2 * (d+gapSize) - d*x1) / gapSize
Similarly, y3 = (y2 * (d+gapSize) - d*y1) / gapSize
Sorry for the math. I didn't try to code it but it sounds right. I hope this helps.
There are many ways to do this. Simplest (to me) is the following. I'll write it in terms of mathematics since I can't even spell C.
Thus, we wish to find the point C = {x3,y3}, given points A = {x1,y1} and B = {x2,y2}.
The distance between the points is
d = ||B-A|| = sqrt((x2-x1)^2 + (y2-y1)^2)
A unit vector that points along the line is given by
V = (B - A)/d = {(x2 - x1)/d, (y2-y1)/d}
A new point that lies a distance of gapSize away from B, in the direction of that unit vector is
C = B + V*gapSize = {x2 + gapSize*(x2 - x1)/d, y2 + gapSize*(y2 - y1)/d}

Calculating the line perpendicular to the midpoint of another line

Essentially I'm trying to calculate the bisector line between two points. I've got two methods, one works the other doesn't. I can't quite figure out why the other one doesn't work. The one that works is a little more computationally intensive and since this routine is run a lot, I'd like to use the simpler one... except it doesn't work. I'm probably missing something simple but I find this amusing since I seem to have a better grasp of trig than I do of high school algebra.
Note: the function is passed the end points (endPoint1, endPoint2).
Here's the one that works (using trig to calculate the bisector):
CGPoint midPoint = CGPointMake((endPoint1.x + endPoint2.x) / 2, (endPoint1.y + endPoint2.y) / 2);
//Normalize an end point
CGPoint nPoint = CGPointMake(endPoint1.x - endPoint2.x, endPoint1.y - endPoint2.y);
//Find theta and rotate 90°
CGFloat theta = atanf(nPoint.y / nPoint.x);
if (nPoint.x < 0.0f) theta += M_PI;
else if (nPoint.x > 0.0f && nPoint.y < 0.0f) theta += (M_PI * 2);
theta += M_PI_2;
//Calculate another point along new theta and de-normalize the point
CGPoint centerPoint = CGPointMake(cosf(theta) * 10, sinf(theta) * 10);
centerPoint.x += midPoint.x;
centerPoint.y += midPoint.y;
//Create the line definition
LineDef def = LineDefForPoints(midPoint, centerPoint);
Here's the one that doesn't, but I'd like it to:
CGPoint midPoint = CGPointMake((endPoint1.x + endPoint2.x) / 2, (endPoint1.y + endPoint2.y) / 2);
//Calculate the slope and invert
CGFloat m = (endPoint1.y - endPoint2.y) / (endPoint1.x - endPoint2.x);
//Take the negative reciprocal
m = -1/m;
//Calculate another point on the line
CGPoint centerPoint = CGPointMake(midPoint.x + 10, midPoint.y + (m * 10));
//Create the line definition
LineDef def = LineDefForPoints(midPoint, centerPoint);
So I'd swear this should work. The change in Y is equal to m times the change in x. I've calculated the mid point, figured out the slope of the perpendicular line and calculated another point on that line. However the line definitions created aren't equivalent when given the same end points, so I'm missing something.
By the way, LindeDef is a simple struct with three CGFloat variables for the a, b & c components of a straight line. And creating a LineDef from two points is trivial (I happen to be using a block to do this):
LineDef (^LineDefForPoints)(CGPoint, CGPoint) = ^LineDef(CGPoint p1, CGPoint p2){
LineDef line = {0,0,0};
line.a = p2.y - p1.y;
line.b = p1.x - p2.x;
line.c = line.a*p1.x + line.b*p1.y;
return line;
};
Slope-intercept form is fragile for this; use vectors.
〈V〉 = B - A
midpoint = 〈V〉/2 + A
⟂〈V〉 = 〈Vy, -Vx〉
Dammit, it was something simple. I reformatted my actual production code so I could put it on Stack Exchange and in the process edited out my mistake. The code I posted in the question actually works perfectly. Originally the line: m = -1 / m; was folded into the original assignment like so: CGFloat m = -1 / (endPoint1.y - endPoint2.y) / (endPoint1.x - endPoint2.x);. Of course, now the problem is obvious...I forgot parenthesis. I separated the line into two in stack exchange so I could explain my 'reasoning' in the comments, hoping to find the problem.
Sorry, for all the trouble.