Essentially I need to return a boolean if a line and a line segment intersect. For the line the information I have is the slope, xy coords for a random point, and xy for a y intercept. For the line segment I have the line segment and the two end point xy coordinates. Any ideas?
The line through (Xr, Yr) with slope S has equation D(X, Y):= (Y - Yr) - S (X - Xr) = 0.
Just check that D(Xa, Ya) and D(Xb, Yb) have opposite signs.
Implementing this is not terribly difficult, it's the conceptual part that seems the most difficult. I might code something up for this for fun later but this should be enough to get you started. Also, note that this is might be a really bad way to solve it (time/space wise) but it will definitely work.
If you want to find a good solution, use convert the line to vectors and use the implementation in the answer in the link below all my text.
Calculate the slope of the second line. If it's equal to the first, they're parallel and never intersect.
Calculate where the two lines would intersect if they do intersect. They can only intersect at one point. This is how you would check if they will intersect:
Find 2 distinct coordinates for each line at some arbitrary point.
Determine the distance between the two lines at each point.
Whichever point results in a smaller distance between the lines represents the direction you need to travel in to move closer to the intersection.
Keep checking the distance between the two lines until they start increasing (which means you need to reverse the direction again) or until the distance is 0. The xy coord at distance = 0 is the intersection.
If the x-value of the point where the two lines intersect is in between the two x-values for your line segment, the line and line fragment intersect.
This should be easier for you because you already have two xy coords for the first line and two end point coordinates for the line segment.
Check this answer out for a really nice solution with some examples in the comments: https://stackoverflow.com/a/565282/2142219
Related
I already got the set of points that define that convex hull. How can I add a kind of padding to them, so that all the points is inside? I am trying to draw the red line
What I'd try to create an outline offsetted from a polygon by distance d:
Consider a straight line segment between each pair of consecutive points (the original convex hull).
Offset each segment by d along its perpendicular to the outside. The offsetted segments have gaps between ends.
For each pair of consecutive offsetted line segments, add a segment of a circle such that it both segments are its tangents; its radius should be d. It's easy to do: draw perpendiculars at the ends of both segments; their intersection is the center of the circle.
Now you have a sequence of interspersed straight lines and arcs. It should be the outline you're seeking.
To work comfortably, you'd need these functions:
Find the equation of the straight line by two points (store it as an object with 3 float fields);
Find an equation of a perpendicular line by a line equation and a point;
Find the point of intersection of two lines, given their equations.
So I'm attempting to calculate if a point is inside of an angle. While researching, I came across many terms I am unfamiliar with. Essentially from a point (A) with a 120° angle protruding from point (A). I want to test if a secondary point (B) would be inside of the angle. All that is known is the degree of the angle and the degree at which the angle is facing and the X and Y values of both points. This will all be done in Java so any and all help is appreciated!
To better explain it:
There is a point with two vectors protruding from said point. The angle that is known is the angle that is created by the protrusion of the two vectors.
First of all, an angle is not defined for two points -- only for two lines.
Define a line that is your 0° 2D-space. For a line, you need a point and a direction.
Calculate the normal-vector for your line (Turn your directional vector by 90°); normalize both your directional and normal vector so that sqrt(x^2+y^2) = 1.
Calculate the distance vector between your initial point and the other point, this is your second line, sharing the same initial point.
Calculate the dot-product of a and b:
a = distance vector × normal vector
b = distance vector × directional vector
Use simple trigonometry to calculate the angle. It's the arctangent of (a/b) or (b/a).
You probably wanna take the absolute value of the result as well if you don't care about left and right.
I am making my own implementation of a raycaster in a game I am making, and I have come across a very hard problem. I have a player (the black dot), and I need to find the intersection nearest to the player. In the image below, the arrow is pointing to the intersection point I need.
What I guess I am trying to say is that I need a function something like this:
// Each line would take in 2 map values for it's 2 points
// In turn, the map would have to have an even number of points
public Point getNearestIntersection(int playerX, int playerY, int lineDir, Point[] map) {
// whatever goes here
}
I am going to have to do this about 50 times every frame, with about 100 lines. I would like to get 40 fps at the least if possible... Even if I divide it up into threads I still feel that it would cause a lot of lag.
The class Point has a method called distance which calculates the distance of two points. You then could loop all points to get the nearest. Could be something like this:
Point currentNearestIntersection;
double smallestDistance;
for (Point inter : intersections) {
double distance = player.distance(inter );
if (distance < smallestDistance) {
currentNearestIntersection= inter;
smallestDistance = distance;
}
}
axis/line intersection is in reality solving:
p(t)=p0+dp*t
q(u)=q0+(q1-q0)*u
p(t)=q(u)
t=? u=?
where:
p0 is your ray start point (vector)
dp is ray direction (vector)
q0,q1 are line endpoints (vectors)
p(t),q(u) are points on axis,line
t,u are line parameters (scalars)
This is simple system of 2 linear equations (but in vectors) so it lead to N solutions where N is the dimensionality of the problem so choose the one that is not division by zero ... Valid result is if:
t>=0 and u=<0.0,1.0>
if you use unit dp vector for direction of your ray then from computing intersection between axis and line the t parameter is directly distance from the ray start point. So you can directly use that ...
if you need to speed up the intersections computation see
brute force line/line intersection with area subdivision
And instead of remebering all intersections store always the one with smallest but non negative t ...
[Notes]
if you got some lines as a grid then you can compute that even faster exploiting DDA algorithm and use real line/line intersection only for the iregular rest... nice example of this is Wolfenstein pseudo 3D raycaster problem like this
I need to plot a group of points based on distances. I have three unknown points X, Y, and Z. I then get another unknown point (A) and its distances from the originals (AX, AY, AZ). I will continue getting points and distances (B, BX, BY, BZ; C, CX, CY, CZ) etc.
My question is whether its possible to plot all of the points. If so, how many points would I need for an exact plot map? What about an approximate map?
This is similar to this question but I get a different set of distances and am not limited to the original number of points.
Also, if it would help I could add more points to the X, Y, Z group which would give me more distances for A, B, etc.What I don't know until it's been somehow calculated are any of the Distances XY, XZ, YZ, AB, AC, etc.
I am not sure exactly what you mean by exact plot map or approximate plot map. I think I might know but I am not sure. But plotting all points in this case, to me is not possible if the user can continue to "add more points to the XYZ group", which is dynamic. It would sound like you need to know what the user is going to plot before he does. Now if all this is static, it is possible
I assume you use 2D space
If it is 1D then 2 points are enough (not identical !!!).
If 2D then 3 distances is enough but the points used must not lay on the same line !!!
position/orientation of the plot
for relative plot are above conditions enough if you want also the exact orientation and position then you need to know exact position of first 3 points otherwise your plot will look the same but can be offseted,rotated and mirrored to original geometry.
knowing 1 point eliminates offset
knowing 2 point eliminates rotation
knowing 3 point eliminates mirroring
[notes]
you need n+1 points for n-D coordinate system
[edit1] equations
original question text did not contain any equations need but comments requires it so here are some:
You will need intersection point between two hyperspheres (in 2D circles, in 3D spheres,...) so look here:
circle-circle intersection
Cast circle from each point as center with radius equal to the distance from that point. Find out intersection point that is the same between all combinations of circles (0,1),(0,2),(1,2)
Yellow intersection is the same in all 3 combinations so that is the next point or for 2D just solve this system:
(x-x0)^2+(y-y0)^2=l0^2
(x-x1)^2+(y-y1)^2=l1^2
(x-x2)^2+(y-y2)^2=l2^2
where x,y is the intersection point, xi,yi are center of circle and li is distance from that point.
The first option should be simpler and more accurate if done right but need some knowledge on vector and trigonometry math. You will need to add rotation or compute on vectors and use perpendicular vector feature in 2D
V(x,y) -> V0(+y,-x),V1(-y,+x)
where V0,V1 are perpendicular to V
How can I draw a line with asterisks on the console? I will accept the coordinates from the user (x1,y1), (x2,y2).
Assuming a reasonably capable console, you can combine the ANSI escape code for Horizontal and Vertical Position (HPV) with Bresenham's line drawing algorithm.
Addendum: As this is homework, Bresenham's algorithm is overkill. Because it's a common assignment, you might look at how others have approached the problem. In addition, you can edit your question to include your code and other details about the assignment.
I would first write a method that prints out a star at a given x-coordinate. Such as given an x coordinate 7, print out 6 spaces and then an asterisk.
I would then determine the slope (y2-y1)/(x2-x1) make sure that x1 is the point with the higher y value(on a higher line, but this might be a lower actual value). Then increment the x value by the slope on each iteration through a loop
for(i = y1; i<=y2;i++)
asteriskprintfunction(x1+(slope*i));
that should do it.
edit:
had accidentally made the loop with x values and since you want to go line by line, you should use the y values. also clarified how to pick which point is (x1,y1)