• #### http://www.ucancode.net

Posted by Legacy on 08/01/2003 12:00am

Originally posted by: MFC Diagramming Library with Source Code!

Great!!

• #### Speedup

Posted by Legacy on 06/17/2003 12:00am

Originally posted by: Alex

One smart optimisation ( from the old days): Divide the XY area in 8 octants( or hexants,or whatever is the decomposition of N in prime factors). Compute the vertices for one and flip/mirror for th others. This will solve partially the problem of rounding and also speed up a lot the code.

The code is very interesting.

Alex

• #### Alternate Code

Posted by Legacy on 07/17/2002 12:00am

Originally posted by: DjinnKahn

```replace the given code snippet with this:

void Draw(CClientDC& dc, CPoint orig, CPoint vertex, int iNumSides)
{
vertex -= orig;
for (int i = 0; i <= iNumSides; i++) {
double fCos = cos(i * 4*acos(0)/iNumSides);
double fSin = sin(i * 4*acos(0)/iNumSides);
CPoint p(	int(vertex.x * fCos - vertex.y * fSin + .5),
int(vertex.x * fSin + vertex.y * fCos + .5));
if (i) dc.LineTo(p + orig);
dc.MoveTo(p + orig);
}
}

void CPolygonsView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_Drag && PointOrigin!=point)// for mouse drag check
{
CClientDC ClientDC(this);// INITIALIZE GRAPHICS
ClientDC.SetROP2(R2_NOT);
Draw(ClientDC, PointOrigin, PointOld, m_Sides);
Draw(ClientDC, PointOrigin, point, m_Sides);
}
PointOld = point;// used to erase the previous drawn figure
CView::OnMouseMove(nFlags, point);
}
```

• #### S.O.S

Posted by Legacy on 09/17/2001 12:00am

Originally posted by: Samy

Can anyone help me please?
To generate a form,

I want to draw "N" rectangle and lines in my GDI and for each rectangle I need to gave a name and save all properties (X1,Y1,X2,Y2) in text file.
Each line must be resizable and tracking(I don't know haw to track a line).
Each rectangle must be resizable and tracking(I already do this with one rectangle).
Each rectangle must be able to receive a text(I already do this with one rectangle).
The properties of each text in rectangle must be interactive changed by user and also saved in text file(I already do this using LbuttonDblclick + fontdialog).

For example:
Myfile.txt
Rectangle name: X1: Y1: X2: Y2: fontsize: fontname:

Title 100 120 200 220 12 Arial
Adres 150 200 200 250 10 Currier
.....

It is posible to decide that I have i.e 10 elements. In this case I must initalize all of them and drawing one to one.
But in this case user will not have any flexibility to add new rectangle any time when he want.

Could any one gave me some good algoritm for that?
Best regards,
Samuel

• #### Rounding-problem ? no problem

Posted by Legacy on 08/22/2001 12:00am

Originally posted by: Dieter Hammer

in th loop You have only to remove the (int) casts, that's all folks!
Px = Px - 2 * x2 ;
Py = Py - 2 * y2 ;

• #### The code is ok..Good

Posted by Legacy on 08/17/2001 12:00am

Originally posted by: James P

Your code is quite ok.But can't you make it more powerful

• #### N-Sided Polygons

Posted by Legacy on 08/16/2001 12:00am

Originally posted by: Craig Moore

I think that it may have been useful to post the fact that you wish to draw n-sided <i>regular</i> polygons, as surely there are scanline rendering algorithsm for drawing polygons already. Also, it may have been useful to post an algorithm for scanlining solid polygons, as they are more useful imho.

• #### Here are some thoughts for you...

Posted by Legacy on 08/15/2001 12:00am

Originally posted by: John

```A better approach might be to have a general n-sided
polygon drawing function, such as:

void DrawPoly( double x, double y, double Radius, double Angle,
int Sides, COLORREF Color )
{
// set the pen and fill colors as needed
// ...
// ...

double Delta = (2*3.14159265) / Sides;

for ( int i = 0; i < Sides; i++ )
{
double nx, ny;

nx = x + ( Radius * cos( Angle + i * Delta ) );
ny = y + ( Radius * sin( Angle + i * Delta ) );

if ( !i )
MoveTo( (int)(x + Radius), (int)y );
else
LineTo( (int)nx, (int)ny );
}
}

I left out the details of setting pen/brush colors, and I
glossed over the actual drawing stuff, but the idea is to
create the function and call it from inside the OnMouseMove
code.  That way, your code is much cleaner, and you can
create n sided poly's from anywhere without having to
copy/paste all over the place.

Remember, C and C++ allow us to create functions which
should be as reusable as possible.  Less code means easier
debugging and usually more efficient operation.

Good luck!

-John

PS: Editing code in this browser window sucks.  :-)
```

• #### its interesting

Posted by Legacy on 08/14/2001 12:00am

Originally posted by: ambrish

```thats a nice and interesting article.
give a lot of insight.
```