PDA

Click to See Complete Forum and Search --> : Click on a line (Linear function)


Zvonko
Jul 18th, 2001, 06:01 AM
I actually found one possible solution, however it doesn't work.
Let's see:

For each line there's a mathematical function with that form:

y = k * x + n

y= y coord; x = x coord; n = number; k = static variable

Following function is the same as above, it just uses 2 coordinates (x, x1, y, y1)

y - y1 = k * (x - x1) ---> y = k * (x - x1) + y1

Point is that here we do not have k, so we have to calculate it. This is done by next function:

k = (y2 - y1) / (x2 - x1)

Now we have k, both coordinates and function for that line. And even now I cannot find out if I clicked on a line.

Any suggestions?

Yonatan
Jul 18th, 2001, 08:49 AM
The function of the line is:
y = kx + n
You clicked on the point: (a, b)
So you clicked on your line, if the following is true:
b = ka + n

Zvonko
Jul 18th, 2001, 11:38 AM
That's true, Yonatan.
However I have two other constants to retrieve/get/find: k and n.
K is retrieved with that function:
k = (y2 - y1) / (x2 - x1)
Then I have to get n. This is for me impossible, so I use another linear function, equivalent to y=kx+n: y - y1 = k(x - x1) or y = k(x - x1) + y1
Like this I take one saved point (start or end coordinate - x,y) and point where I clicked (x1,y1).

And it still doesn't work.

Suggestions are welcome

kedaman
Jul 18th, 2001, 12:37 PM
You're probably not clicking on the line, and might be a bit hard to do so. Instead you could try accepting clicks close enough to the line. You can get the distance to the line by squarerooting the difference between the length of vector AC and dotproduct of AB and AC. compare a scalar constant D which specify the max distance from the line with the root, to retrieve validation.

Zvonko
Jul 18th, 2001, 04:41 PM
Good answer, kedaman...

That's what I wanted to do..

However... would you be so kind and post an example here? I'm from Slovenia and english mathematical terms are not very familiar to me... :(

kedaman
Jul 19th, 2001, 07:19 AM
perhaps you understand mathematical symbols isntead. I think you could use the crossproduct instead.

| AC x AB |
_________ < D
|AB|