PDA

View Full Version : Converting MFC CDC::Arc(..) to QPainter->drawArc



maverick_pol
24th September 2007, 10:08
Hi guys,

I have to convert MFC code to Qt.
I have a problem with creating arcs.

I have the MFC code:


pDC->Arc(upperLeft.x, upperLeft.y, lowerRight.x, lowerRight.y, dxStart, dyStart, dxEnd, dyEnd);

Here are docs for MFC arc:
http://msdn2.microsoft.com/en-us/library/0ek4s1k5(VS.80).aspx
I need to paint the arc using this Qt method:


void QPainter::drawChord ( const QRectF & rectangle, int startAngle, int spanAngle )


But I have a problem with startAngle/span Angle. I have the coordinates(dxStart.....DyEnd), but don't know howto interpret them to create a startAngle in the Qt method.

Any ideas? Thanks

Maverick

wysota
24th September 2007, 10:57
Seems that this is a simple trygonometric issue. In MFC you pass the end point and in Qt you pass the end angle. The point lies on the line that crosses the horizontal "diameter" with the angle specified. Thus knowing the horizontal and vertical distance of the point from the horizontal "diameter", you can use arcus tangent to calculate the angle.

tg(alpha) = y/x => alpha = arctg(y/x)

I hope I didn't mix tangent and cotangent, but this is your business to check that ;)

maverick_pol
24th September 2007, 11:31
Thanks,

Ok so I can get the StartAngle using trygonometric function.
But which angle is the SPANangle?
Thank you for you help.

Maverick

wysota
24th September 2007, 11:56
Span is the difference between end angle (which you can calculate in a similar manner) and start angle. Just remember that tangent and cotangent operate on (-pi/2; pi/2) range, thus you might have to calculate the final angle by watching the signs of arguments, like taught in school ;)

maverick_pol
25th September 2007, 13:43
Hi,

I have tried using the hints you have given me and the situation is not so bright. Yes you are right, that some simple math methods can be used, but it does not solve my problem. Here is the link that show how to use MFC method CDC::arc(...)
http://www.ucancode.net/faq/CDC_Arc_Pie_Chord.htm

What interest me is how can I know, having start/end points and the rect, what is the inclination(slope) of the line. It is possible to an infinite number of arcs having the bounding rect and start/end point, am I right?

I am stuck and do know know what to do now.

Before hand thank you.

Maverick

wysota
25th September 2007, 14:18
The slope is determined by the tangent of the end point. There is only one arc (defined as a portion of an ellipse) with given bounding rect and end points. I wanted to make a drawing, but I seem to lack an idiot proof enough tool to do it here (anyone knows how to draw a straight line in Karbon?).

maverick_pol
25th September 2007, 16:03
Hi,

I tried converting this from MFC to Qt:

pDC->Arc(20, 20, 226, 144, 202, 115, 105, 32);

but can't get a good result.

If you have time, please help with this one.

Thank you.

wysota
25th September 2007, 17:17
Here you go. I must have made some mistake as the arc is a bit wrong, but it's probably a matter of adjusting something. If you look at the result, you'll see that the coordinates of end points (red and green dots) are not exactly on the arc. I can't explain that - either MFC arc drawing works differently (maybe its not an arc but a curve?) or coordinates are extrapolated to match the bounding rect.

maverick_pol
26th September 2007, 08:57
Hi,

Thanks for the code I will try to test it in my app. As I remember, the msdn docs point that the start/end points do no have to lay exactly on the arc.

The arc looks fine.

Thanks