PDA

View Full Version : Regarding Painting



archanasubodh
6th August 2008, 13:37
There exists a line with the coordinates of the 2 end points known(say P and Q)

For a shortened line the following steps were used


Determine and slope(m) and intercept(c)
m and c represent the standard notations in the equation of a line y=mx+c.

Then P.x()+20(pixels)

Determine relevant "y" on the same line using the equation y= mx+c where
m and c are known from previous calculations.

There is a serious problem in this approach because the slop is prone to "tangent" behavior i.e as the slope increases from 0 to 90 it increases and at 90 it becomes infinity.


Please do help me calculate the coordinate of a point on the line such that the line is shortened.I should be able to draw a shortened line from the new position.


Thank You.

jacek
6th August 2008, 21:41
Try the vector approach:

u.x = Q.x - P.x
u.y = Q.y - P.y
if( u.x != 0 ) {
u *= 20 / u.x
P += u
}
else {
// impossible
}

archanasubodh
7th August 2008, 06:45
Hello

I'm not sure how to approach this problem using vectors.Please do let me know in a little detailed manner.

jacek
7th August 2008, 13:58
Which part you don't understand?

archanasubodh
8th August 2008, 10:36
How to use Vectors in my case?Is it to represent QPoints?
How will it help me solve the tangent behavior?

jacek
8th August 2008, 17:24
How to use Vectors in my case?Is it to represent QPoints?
I'm talking about mathematical vectors (like the "u" from the pseudocode snippet above).


How will it help me solve the tangent behavior?
You can't avoid it, but computations with vector approach will be easier.

fullmetalcoder
8th August 2008, 18:23
well of course if your line is vertical the slope is infinite and you cannot "shorten" it the way you're trying to do... You can check that just like jacek suggests.

mathematically speaking a vector is a "difference" of two points or a "movement" to go from a point to another (purely graphical explanation here. my math teacher would strangle me if he were to read this ;))

code-wise, a vector and a point are exactly the same thing : a bunch of coordinates. The only difference is the way you use them.

proceed like this :


compute the vector that directs your line by substracting the two extremities
normalize it (i.e divide it by its length)
multiply it by the desired length (here 20 pix)
add it back to the starting point of your line to obtain the new interpolated point on the same line


QPointF vec = end - start;
vec /= sqrt(pow(vec.x(), 2), pow(vec.y(), 2));
vec *= 20;
QPoint interpolated = start + vec.toPoint();The use of a QPointF (http://doc.trolltech.com/latest/qpointf.html) here is only *recommended* for more precision. A less accurate but faster version would be :

QPoint vec = end - start;
vec /= vec.manhattanLength();
vec *= 20;
QPoint interpolated = start + vec;

edit : the sample code uses Qt but the method described here is not dependent on Qt (I'd have written this in plain C++ but I didn't feel like declaring my own Point class before ;))