Calculate point of intersection in bounding box - ruby

I have two x,y pairs that create a line within a bounding box.
coord1 = 75, 180
coord2 = -30, 300
The bounding box is x0 to x500 and y0 to y400
I want to create an object that can tell me the coordinates of where the line intersects the bounding box.
i.e.
Intercept.new(bounding_box, coord1, coord2).call! returns the intercept point [x,y]
I believe I need to use y = mx + b, but I'm having trouble writting an object that can take these two coordinates, factor in the bounding box, and tell me where the intersection point happens. Can anyone take a shot and help me out here?
EDIT Not a duplicate of the question linked in the comments. That question has a constant of the point B always being in the center of the rectangle.

You're on the right track with utilizing y = mx + b concepts, and some further linear algebra will be required to solve the problem exactly as you're drawing it. However, you stated you were just looking for direction on where to start for approaching this particular problem.
It seems someone ran into a similar problem regarding projectile intersections while developing a game that may be relevant to your struggles. Here's his blog post: http://factore.ca/blog/166-how-to-calculate-the-point-of-intersection-between-a-line-and-a-bounding-box
Here's a link to his ruby specific solution to his problem: https://github.com/adriand/intercept-calculator/blob/master/intercept_math.rb
Hope this helps!

Related

matlab image processing- line passing through 2 points extending beyond the x1,y1 coordinate

I have a problem to solve in matlab. I have 2 points A[x1,x2] and B[y1,y2] of my particles in Image. i want to plot a line between the 2 points and extend it beyond the x1,y1 coordinate alone.
i later check where it intersects.This will tell me where the particle originates.
is there some simple code for this?
thank you
regards
Avinash
You should narrow down your problem a bit, but I'll suggest creating a C point in the same line as A and B, and as far from A as you wish (say, k times the distance between A and B); then plot the line between C and B.
xC=xA+k*(xA-xB);
yC=yA+k*(yA-yB);
As for finding the intersection (if it is with another line), you can solve it on paper quite easily then implement it on Matlab.

Intersection area of polygon and circle

I want to calculate the Intersection area of circle and a polygon(not self intersecting). is there any good generalized algorithm.
Note:
What I have tried: At first I try to solve the problem with Ray casting algorithm, where I will find the points in the circle and then identify the area. But it seems harder for me as the situation got harder and with complex test cases.
Input Specification : center(x,y) and radius of the circle. vertex of the polygon( {x1,y1} , {x2,y2} , {x3,y3} ... ).
UPDATE: The more I think I become confused. is it really possible to calculate this ?
There are two steps. First, notice that this problem is easy for triangles - as there are no gotchas. Second, notice that you can break up any N-polygon into N triangles. This makes the situation much easier.
Any circle is described by the formula x^2 + y^2 = r^2. So you can get the area to the axis for any region of the line through integration.
I'll fill this out more tomorrow, I'm going out now.

Gradient Of Bezier Curve At Given Point

I cant seem to figure out how to calculate the incline of a curve for the following situation...
Essentially what I am trying to do is increase the speed of an object based on the incline of the curve at a particular point. The speed will be reduced if the incline is upwards and increase if downward.
I was using the derivative of a point t on the bezier curve to establish the tangent but this doesnt seem to be right as I would expect that value to be negative if the slope is downward.
I have been using the below equation for the tangent to evaluate X, Y and Z but then I only use Y to establish the incline...I think that step may be wrong
Any ideas?
EDIT:
Ultimately this is an object moving along an inclined plane but I cant establish the angle of the plane in order to do this, I believe if I could correctly find the angle it may solve the problem. I tried to take the point in question and then another point in front (so for example t = 0.5 and then a point in front would be t=0.51) and then calculate the angle using tan. I completely ignore the Z axis but is that wrong? If not how should I calculate the angle?
Thanks a lot
This should help: http://www.physicsclassroom.com/Class/vectors/U3L3e.cfm .
Essentially, you need to calculate the angle of inclination. If the angle is \theta , then the acceleration depends on sin(\theta).
I am assuming z as the vertical dimension.
if dx,dy and dz is are the gradients in each directions, dw = sqrt( dx^2+dy^2). \theta = tan_inverse( dz/dw). Acceleration = g*sin(\theta).
NOTE: You can directly calculate sin(\theta) without explicitly calculating \theta. sin(\theta) = dz/sqrt(dx^2+dy^2+dz^2).
=== More formal description ===
Let x be the east-west dimension, y be the north-south dimension and z be the up-down dimension.
Let z = F(x,y) give the elevation of the terrain at any given location x,y.
Calculate dz/dx = fx(x,y) and dz/dy = fy(x,y), the partial derivatives of z w.r.t to x and y.
Now, sin(\theta) = dz/sqrt(dx^2+dy^2+dz^2) = 1/(sqrt( (dx/dz)^2+ (dy/dz)^2 )= 1/(sqrt( (1/fx(x,y))^2, (1/fy(x,y))^2 ).
This is how you calculate sin(\theta).
The value of derivation is negative when the slope is "downward". And yes, the derivation is the tangent of the slope angle. Only you should pay attention to the directions. They can change the sign, of course. Only you should take dy/dx, not dy/something else. That is all on 2d curves.
You mention Z in the last paragraph. You curve is 3D? Then, of course, the term "derivation" should be put more precisely. Derivation of what to what do you need? The second idea is - please, explain better, what do you want. BTW, maybe after you write down the task correctly, you'll see the solution as obvious.
If it is 3D, let us say, you have your curve as 3 functions of x(t), y(t), z(t). then you need dz/dq, where dq= dt*sqrt((dx/dt)^2+(dy/dt)^2). Obviously, isn't it?
As I said, no maths here. Merely Pythagor's theorem and proportions. (I take z as height)
Addition: it can be rerecounted as tan(a)=dz/(dt*sqrt((dx/dt)^2+(dy/dt)^2)) => tan(a)=(dz/dt)/sqrt((dx/dt)^2+(dy/dt)^2)) ==> a=ATAN((dz/dt)/sqrt((dx/dt)^2+(dy/dt)^2)). But look out for directions you are moving! They can reverse the sign. For under sqrt(^2+^2) we have lost the direction of the dt proection.

Shape casting a capsule against convex polyhedra

Let's say I have a upright capsule shape (swept sphere) that I would like to cast it along a velocity vector. I would like to be able to find the point of contact and a surface normal for any convex shapes it would intersect along this path. I would also like to find the distance the swept caspule traveled to the point of first contact.
Heres a quick diagram of a capsule being casted against a large convex polyhedra (only one face is drawn)
What kind of algorithm or process could do this? I assume it would be similar to a sphere-cast, but i can't find much on that either.
Since you are considering capsules and convex polyhedra, I suppose you could use something based on GJK. You would get the point of contact and a surface normal during a collision, and the minimum distance between the objects and the associated witness points if there is no collision.
You can also take a look at this publication on Interactive and Continuous Collision Detection for Avatars in Virtual Environments.
Right if its the same as your diagram then finding where it collides is the easy part. Get the circles x and y coordinates and plus '+' the radius of the circle. If that point is at the line of the path then its a collision. The line will have to be found using the line equation here y = mx+c.
The distance can be calculated by setting an intial values of x and y. and then when the object hits set final variables to x and y again. then just the lenght of a line formula to calculate the distance travelled.
The problem is im going on what i know from C++ and i dont know what your programming in.
i think you wanted something else but cant work out what that is from paragraph.

elastic / snaking line algorithm

I am making a graphics application in which I can edit a polyline by dragging the control point of it.
However, I'd like to make it a bit easier to use by making it elastic; When dragging a control point, instead of moving a single point, I'd like the points within a certain distance of that point to be moved as well, depending on how hard the control point is 'pulled'.
Does anyone know a simple algorithm for this? It may be quite rudimentary, as the primary requirement is speed.
Actually, knowing how to call such behaviour would also be nice, so I can look it up on google. I tried 'snaking' line, but that seems to refer to active contours, which isn't what I'm looking for.
Thanks
At a simple level you could achieve this with a little help form Hooke's law. You can basically view your polyline as a string, made up of a load of vertices connected by springs:
o-o-o-o-o-o-o-o-o
Each vertex is connected to another vertex by a spring, which will contract if stretched and repel if squashed.
So, when a control point is moved, the connected springs will either expand (stretch) or contract (shrink). This, in turn applies a force to any vertex sharing that spring. So if I pulled the first vertex up and left, the spring would apply a force to the vertex to the right, drawing it closer. This continues to the next (with some energy dissipating) until all springs are 'comfortable'.
That's the basics of it, each time a control point moves you need to solve the equation to all vertices/springs and the points will 'snake' for you.
If you want further examples google for 'Rope Physics' or 'Cloth physics' (as a rope is a 1D cloth). Ignore gravity for your purposes though, obviously.
Basically you are looking for a method to move (deform/transform) multiple points.
Let's assume you have given the direction and strength of the move which would result in dx, dy for the point x, y.
Your transformation will at least two more parameters
The radius r in which the points will be affected
Since the points in the middle would be affected more and at the edge you should define how to interpolate the falloff (linear, normal distribution, etc...)
For linear interpolations the points that are affected would move according to the following formula:
r[i] = sqrt(sqr(x-x[i])+sqr(y-y[i]))
so if r[i] < r
x[i]' = x[i] + dx*(1-r[i]/r)
y[i]' = y[i] + dy*(1-r[i]/r)
this is for linear interpolation dx[i] = dx - r[i]/r

Resources