Solved

# Good points to write this drawing routine for me :)

Posted on 2005-05-02
149 Views
I have simple drawing code that does MoveTo() followed by LineTo() to draw a single line from point A to point B. it can be horizontal, vertical or any angle in between.

each line has a numerical value corresponding to a distance, associated with it.

I need code that will draw this value, just assume it's always "1.00" - I'll plug in the variable later, but here's the thing - the string must be drawn parallel to the line, centered between the two end points, and just a couple pixels above it.  Make sense I hope?
0
Question by:PMH4514

LVL 55

Expert Comment

You have to create a font for each label you write.

DrawLineWithLabel(CDC *pDC, CPoint from, CPoint to, LPCTSTR label)
{
CFont font, *oldFont;

pDC->MoveTo(from);
pDC->LineTo(to);

// Calculate angle in decimals of degree, maybe some sign problems, please check
float angle = atan2(to.y-from.y, to.x-from.x) / 0.0174532925199433 / 10;

font.CreateFont(
10,                        // nHeight
0,                         // nWidth
0,                         // nEscapement
angle,                         // nOrientation: here is angle used
FW_NORMAL,                 // nWeight
FALSE,                     // bItalic
TRUE,                     // bUnderline
0,                         // cStrikeOut
ANSI_CHARSET,              // nCharSet
OUT_DEFAULT_PRECIS,        // nOutPrecision
CLIP_DEFAULT_PRECIS,       // nClipPrecision
DEFAULT_QUALITY,           // nQuality
DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
"Arial");                 // lpszFacename

oldFont = (CFont *)pDC->SelectObject(&font);
pDC->SetTextAlign(TA_CENTER|TA_BOTTOM);
pDC->TextOut( (to.y+from.y)/2, (to.x+from.x)/2, label, strlen(label));
pDC->SelectObject(oldFont);
}
0

Author Comment

if there is already a font selected into the DC, do I still have to do all that font stuff?     it was more the angle calculation stuff I was looking for, which I see you did, I'll give that a try!
0

LVL 55

Expert Comment

>if there is already a font selected into the DC, do I still have to do all that font stuff?
YES, every angle is a different font
0

Author Comment

ok n/m that font comment, the answer I found is yes.

anyway - I think your angle calc is off.  I changed it to this to fit my code, as I don't actually have a "point" (sorry, bad phrasing) -rather two ints for x/y but otherwise the same:

this code:
float angle = atan2(m_iEndY-m_iStartY, m_iEndX-m_iStartX) / 0.0174532925199433 / 10;

memDC->SetTextAlign(TA_CENTER|TA_BOTTOM);
memDC->TextOut( (m_iEndY+m_iStartY)/2, (m_iEndX+m_iStartX)/2,  label, strlen(label));

lets see if I can describe what's happening. This code is run as the mouse moves, and the line that's being drawn is updated. So I click and start creating my line, and the label is displayed at what appears to be a random location on screen.  Then as I extend my line to the right, the label stays put horizontally, but goes down vertically.. extending the line to the left, the label stays put horizontally, but goes up vertically.

extending my line down, the labels moves to the right, and up to the left..

0

LVL 55

Expert Comment

memDC->TextOut( (m_iEndY+m_iStartY)/2, (m_iEndX+m_iStartX)/2,  label, strlen(label));
I think must be:
memDC->TextOut( (m_iEndX+m_iStartX)/2, (m_iEndY+m_iStartY)/2,  label, strlen(label));

0

Author Comment

hmm.. well, it follows the middle of the line at least..

but, the text itself is always horizontal rather than being "rotated" so that it is drawn parallel with the angle of the line.

is it not possible?

ps. from where did you derive that "0.0174532925199433" value  in the atan function?
0

Author Comment

oh - wait a sec.. I see now why you needed to create the font, that is where you specify the calculated angle.. this should work then
0

Author Comment

ok I spoke to soon :)

it's still not working.. all the font attributes are set, but the text still displays horizontally. do I need a specific map mode set?
0

LVL 55

Expert Comment

2.PI / 360 = 0.0174532925199433
0

LVL 55

Expert Comment

have you verified angle value? it must be a value between -1800 and +1800

Hmmm, I see, should be:
float angle = atan2(m_iEndY-m_iStartY, m_iEndX-m_iStartX) / 0.0174532925199433 * 10;

because angle must be specified in 0.1-degree units
0

Author Comment

oh times 10.. gotcha..

lets see.....

nope, still not working.. yes, all the angles are -1800 and +1800 and they look to be correct.. when my line goes straight upward, I show an angle of -900  and when it goes straight down, angle=900.. straight out to the right = 0 and to the left = -1800

but the text is always horizontal
0

LVL 55

Accepted Solution

reviewing documentation............
I see, appear to be that need this too:

font.CreateFont(
10,                        // nHeight
0,                         // nWidth
angle,                         // nEscapement <----------------------------------------- needs angle too
angle,                         // nOrientation: here is angle used
FW_NORMAL,                 // nWeight
FALSE,                     // bItalic
TRUE,                     // bUnderline
0,                         // cStrikeOut
ANSI_CHARSET,              // nCharSet
OUT_DEFAULT_PRECIS,        // nOutPrecision
CLIP_DEFAULT_PRECIS,       // nClipPrecision
DEFAULT_QUALITY,           // nQuality
DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
"Arial");                 // lpszFacename

And, of course, you have to use a truetype font.
0

Author Comment

actually I did try setting escapement to angle a bit earlier.. but then I set it back when I saw  no change.. however I then changed to *10 so let me try that again

...

closer! now the angle is 90degrees off..

...  changed *10 to * -10   (is that reasonable?)

now it works as I was looking for..  I see now though that I will need to check if the angle is between 900 and -900 and "Reverse it" so that I don't have to turn my head upside down to read it :)
0

LVL 55

Expert Comment

> I see now though that I will need to check if the angle is between 900 and -900 and "Reverse it" so that I don't have to turn my head upside down to read it :)
Yeah, this is a painful issue...
0

Author Comment

>>Yeah, this is a painful issue...
yeah, suddenly I'm seeing this..

got any pointers?
0

LVL 55

Expert Comment

Check the range of angles you don't like and add it 1800 to invert.
0

Author Comment

oh duh.. add 1800.. . here I'm messing with trying to multiple by -1 and such..

nice! got it :)
0

LVL 55

Expert Comment

Uff, finally! it is lunch time... see you in next question
0

Author Comment

:)
0

## Featured Post

### Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual â€¦
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar â€“ letâ€™s display the timestamp there.  We need to get the timestamp from the document sâ€¦
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.