PDA

View Full Version : Shortenning a line



archanasubodh
9th June 2008, 10:57
Hello

I have 2 QPoints(P,Q) from which i construct a straight line(inclined/horizontal/vertical).
In some scenarios the length of the line should be reduced.Please do suggest a method to shift the point P so that i can reduce the length of the line.


We have P(x1,y1) Q(x2,y2)
Currently we use the formula, y = mx + c and get the values of y(slope) and c intercept.
Then assuming that the new point is 10 pixels horizontally from one end of the line.
Determine the new y` which also lies on the line y=mx+c.
We get the new P`(p.x()+10,y`).


This method does not work for all cases.
Please do let me know a good way to reduce the length of the line.

Thanks in advance.

lyuts
9th June 2008, 13:15
You are right, this method is not working for all the cases. The problem is with step #3. What you should do first is try to determine the kind of your line, it means to figure whether P is to the left from Q, or to the right, and is P above Q or below.

For example, let's say we have P(p1, p2) and Q(q1, q2).


Case 1:
p1 < q1 and p2 < q2

Your line will look like this



. * Q
/
/
/
* P


It means that your new point P` will be above P. That;s why you take P.x() + some_value and then calculate new value for P.y() from that equation.



Case 2:
p1 > q1 and p2 < q2




Q *
\
\
\
* P

in this case you'll have to take P.x() - some_value.


And so on, and so forth...

I hope you get the idea.

archanasubodh
9th June 2008, 14:27
Hi

Consider the following case:
P(135,191) Q(140,59)
The slope = -26.4
c = 3755

Now calculating P`y= -26.4(135+5) +3755
will give you P`y=-59, which happens to be in the opposite direction.

PS:We paint on a canvas where the top left corner is considered as the origin.


Thank in advance.

lyuts
10th June 2008, 08:57
Yes, you are right, my last post was written taking into consideration that the points are positioned on regular co-ordinates, but not on screen. For screen these coordinates should be flipped.

Here's your case

Hi
P(135,191) Q(140,59)
The slope = -26.4
c = 3755

Now calculating P`y= -26.4(135+5) +3755
will give you P`y=-59, which happens to be in the opposite direction.

Thank in advance.

Let's calculate P`y which is P`y=-26.4*(135+5)+3755=-26.4*140+3755=-3696+3755=59 but not -59. So it works.

archanasubodh
10th June 2008, 12:30
Consider this example,where i get a line in the opposite direction.
P(400,267) and Q(401,119)
Here calculating the slope "m",we get -148.
The y intercept "c" = 59467.
Then deducing P`y=-148(403) + 59467
59467 - 59644
P`y = -177.

Hence i get a line in the opposite direction as that of the original.

PS:With the variation of the angle,the length of reduction also changes.
For instance,
When the line is inclined at an angle less than 60 the spacing is less, for lines whose inclination > 60 to 90 the spacing increases and for angles close to 90 or 270 and line goes in the opposite direction stretching to infinity.
The behavior is similar to that of the tangent of an angle.
Please do give me a convincing solution.


Thanks in advance.

lyuts
11th June 2008, 09:11
Again, in this case your Q point is above P and to the right from P (on a screen)

and when you do P`y=-148(403) + 59467 you, in fact, are not shortenning it. You are making it longer, since you are taking a new point with x=403 which will be above Q and to the right from Q.

The first thing you should do before applying your shortening algorithm is find out the orientation of your line.