How to finding X, Y position...

Rui Caldas's icon

Hi there

Because I'm not so good at English so, I will explain the problem with an image.

(download please)

How do I can find the X, Y position of the pink and blue point with the given data?

Given data is from two faders "green and orange" at the center, that will be related with the "big black dots" (top one to green and right one to orange)

This problem is only "how to find intersseption point from two lines" and "how to find possible intersseption point from two lines that do not intercept each other"

this is probably something simple, but I'm not so good at math to solve this trigonometric question!

Thanks in advance for any tip!

Peter Castine's icon

No trigonometry involved, it's just a pair of linear equations for each point you seek.

For the pink point, the two equations are

EQ1) y1 = 7 - (14/3) * x1
EQ2) y2 = 3 - (3/7) * x2

You want to find the point where (x1, y1) = (x2, y2). To find this point you use the fact that the right hand sides of both equations must be equal at the point you're looking for, so solve for x in the equation

7 - (14/3) * x = 3 - (3/7) * x

That gives you a number: x = 84/89 ~= 0.9438

Then plug the value back into either of the original equations to solve for y (2 53/89 ~= 2.5955).

The blue point is left as an exercise for the reader. Ditto for turning this into a patch.

You may want to read the article at Wikipedia on Linear equations. There are versions in over 30 languages, hopefully one of them will make sense.

--
Edit: minor clarification in the text.

seejayjames's icon

RFCaldas wrote on Fri, 20 March 2009 21:43

How do I can find the X, Y position of the pink and blue point with the given data?

This problem is only "how to find intersseption point from two lines" and "how to find possible intersseption point from two lines that do not intercept each other"

Also, if you don't need to be exact, you can utilize the GL commands in jitter to approximate: each point uses a jit.gl.sketch and the "lineto" command to draw a line from the point to any desired other point, creating two lines (like you have going to your axes), then you can see the intersection visually. You won't be exact but having a jit.gl.gridshape underneath it all (with correct scaling and dimensions) will get you pretty close, and you can try all kinds of combinations easily, even higher-power equations or sine etc., if you create functions to generate the points (uzi and expr together).

If the gridshape is 20 X 20 you'll have from -10 to 10 on each axis, fairly accurate, or you can have 200 X 200 and be 10X more precise. Note that you need to scale it correctly for the points in the grid to be at the right numerical values. To really see where the intersection is you can use jit.gl.handle to reposition the scene closer, or send a camera 0 0 $1 message to jit.gl.render, where low values of $1 mean a very close z-position.

One nice thing about this is that once you've got the jit.gl.sketch functions working, you can move/rotate/scale them arbitrarily after the fact, without worrying about what's happening to the equations. So you could take two lines which don't intersect on-screen, scale each proportionally, and eventually they will intersect if they're not parallel; then zoom out to see where this happens, scale the gridshape to accommodate, and estimate the point.

This is all of course considerably more work, but can allow for more flexibility if that's what you need, and it might generate some ideas along the way.

The image:
y = x (red) and y = -2x - 10 (white)
Approx. intersection by visual: (-3.4, -3.4)
Actual intersection by solving the equations: (-10/3, -10/3)

Since you can zoom in arbitrarily close and can make the gridshape as dense as you want, you can get really good approximations if you need them. also it's fun to experiment with.

index.php?t=getfile&id=2675&private=0

Luke Hall's icon

Here's a javascript that will do all the hard work for you. It accepts an 8 element long list in the form of 4 x,y pairs. The first two pairs being points on line one, the second two pairs being points on line two. It outputs the point of intersection as a list. I really would recommend reading up on the maths so you know exactly what is going on in this patch. Follow Peter's advice and find some articles on linear equations.

lh

// lh.intersect.js
function list()
{
all = arrayfromargs(arguments);
m1 = (all[3]-all[1])/(all[2]-all[0]);
m2 = (all[7]-all[5])/(all[6]-all[4]);
b1 = all[1]-(m1*all[0]);
b2 = all[5]-(m2*all[4]);
x = (b1-b2)/(m2-m1);
y = (m1*x)+b1;
outlet (0, x, y);
}
// EOF

Rui Caldas's icon

Hi

Many thanks!

The English version on Wikipedia is not so clear for me and the Portuguese one is very poor also! I think I've understand... but not for sure!

Anyway...

Using "Hope's" JS:

// lh.intersect.js
function list()
{
all = arrayfromargs(arguments);
m1 = (all[3]-all[1])/(all[2]-all[0]);
m2 = (all[7]-all[5])/(all[6]-all[4]);
b1 = all[1]-(m1*all[0]);
b2 = all[5]-(m2*all[4]);
x = (b1-b2)/(m2-m1);
y = (m1*x)+b1;
outlet (0, x, y);
}
// EOF

...and some geometry facts, I was able to transform the XY solution into an XYZ solution!

I think it's working well, but you can see if I'm not wrong!

Using two "geometric projections" (top and side view) of the two lines I was able to trace XY in one view and XZ in the other, and then, combining the two results, I get the XYZ solution!

In the zip file is an image with the experimental drawing and the patch to calculate XYZ.

Next, I will try to code that directly inside JS. I don't know the JS language at all, but that is very simple to understand (I guess) and I will have no problem adding new arguments...
I think that is a good exercise...

Once again, thanks alot!