Good points to write this drawing routine for me :)

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?
PMH4514Asked:
Who is Participating?
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
PMH4514Author Commented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Jaime OlivaresSoftware ArchitectCommented:
>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
 
PMH4514Author Commented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
PMH4514Author Commented:
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
 
PMH4514Author Commented:
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
 
PMH4514Author Commented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
2.PI / 360 = 0.0174532925199433
0
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
PMH4514Author Commented:
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
 
PMH4514Author Commented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
> 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
 
PMH4514Author Commented:
>>Yeah, this is a painful issue...
yeah, suddenly I'm seeing this..

got any pointers?
0
 
Jaime OlivaresSoftware ArchitectCommented:
Check the range of angles you don't like and add it 1800 to invert.
0
 
PMH4514Author Commented:
oh duh.. add 1800.. . here I'm messing with trying to multiple by -1 and such..

nice! got it :)
0
 
Jaime OlivaresSoftware ArchitectCommented:
Uff, finally! it is lunch time... see you in next question
0
 
PMH4514Author Commented:
:)
0
All Courses

From novice to tech pro — start learning today.