4
votes

I am trying to draw a circle and I am currently using the Ellipse() function.

I have the starting mouse coordinates - x1 and y1 and the ending coordinates x2 and y2. As you can see, I am forcing the y2(temp_shape.bottom) to be = y1+(x2-x1). This doesn't work as intended. I know the calculation is completely wrong but any ideas on what is right?

Code Below.

case WM_PAINT:
        {

            hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code here...
            RECT rect;
            GetClientRect(hWnd, &rect);

            HDC backbuffDC = CreateCompatibleDC(hdc);

            HBITMAP backbuffer = CreateCompatibleBitmap( hdc, rect.right, rect.bottom);

            int savedDC = SaveDC(backbuffDC);
            SelectObject( backbuffDC, backbuffer );
            HBRUSH hBrush = CreateSolidBrush(RGB(255,255,255));
            FillRect(backbuffDC,&rect,hBrush);
            DeleteObject(hBrush);



            //Brush and Pen colours
            SelectObject(backbuffDC, GetStockObject(DC_BRUSH));
            SetDCBrushColor(backbuffDC, RGB(255,0,0));
            SelectObject(backbuffDC, GetStockObject(DC_PEN));
            SetDCPenColor(backbuffDC, RGB(0,0,0));



            //Shape Coordinates
            temp_shape.left=x1;
            temp_shape.top=y1;
            temp_shape.right=x2;
            temp_shape.bottom=y2;



            //Draw Old Shapes
            //Rectangles
            for ( int i = 0; i < current_rect_count; i++ )
            {
                Rectangle(backbuffDC, rect_list[i].left, rect_list[i].top, rect_list[i].right, rect_list[i].bottom);
            }
            //Ellipses
            for ( int i = 0; i < current_ellipse_count; i++ )
            {
                Ellipse(backbuffDC, ellipse_list[i].left, ellipse_list[i].top, ellipse_list[i].right, ellipse_list[i].bottom);
            }

            if(mouse_down)
            {
                if(drawCircle)
                {

                    temp_shape.right=y1+(x2-x1);

                    Ellipse(backbuffDC, temp_shape.left, temp_shape.top, temp_shape.right, temp_shape.bottom);
                }

                if(drawRect)
                {
                    Rectangle(backbuffDC, temp_shape.left, temp_shape.top, temp_shape.right, temp_shape.bottom);
                }

                if(drawEllipse)
                {
                    Ellipse(backbuffDC, temp_shape.left, temp_shape.top, temp_shape.right, temp_shape.bottom);
                }
            }

            BitBlt(hdc,0,0,rect.right,rect.bottom,backbuffDC,0,0,SRCCOPY);
            RestoreDC(backbuffDC,savedDC);

            DeleteObject(backbuffer);
            DeleteDC(backbuffDC);
            EndPaint(hWnd, &ps);
        }
        break;
3
Could you give a bit more than "doesn't work as intended"?chris
It doesn't draw a proper circle.user1788175
@user1788175: What does it draw?AnT
@AndreyT an ellipse. EDIT - Now drawing a circle after Remy's answer but the functionality is limited.(see below)user1788175

3 Answers

2
votes

If you want Ellipse() to draw a perfectly round circle, you need to give it coordinates for a perfectly square shape, not a rectangular shape.

Assuming x1,y1 are the starting coordinates of the dragging and x2,y2 are the current mouse coordinates, then try this:

//Shape Coordinates
temp_shape.left = min(x1, x2);
temp_shape.top = min(y1, y2);
temp_shape.right = max(x1, x2);
temp_shape.bottom = max(y1, y2);

...

if (drawCircle)
{
    int length = min(abs(x2-x1), abs(y2-y1));

    if (x2 < x1)
        temp_shape.left = temp_shape.right - length;
    else
        temp_shape.right = temp_shape.left + length;

    if (y2 < y1)
        temp_shape.top = temp_shape.bottom - length;
    else
        temp_shape.bottom = temp_shape.top + length;

    Ellipse(backbuffDC, temp_shape.left, temp_shape.top, temp_shape.right, temp_shape.bottom);
}
2
votes

I have worked out a calculation which works better. Pasted below for anyone else wanting the same.

if(drawSquare)
                {

                    int xdiff = abs(x2-x1);
                    int ydiff=abs(y2-y1);

                    if(xdiff>ydiff)
                    {
                        if(y2>y1)
                            temp_shape.bottom=y1+xdiff;
                        else
                            temp_shape.bottom=y1-xdiff;
                    }
                    else
                    {
                        if(x2>x1)
                            temp_shape.right=x1+ydiff;
                        else
                            temp_shape.right=x1-ydiff;
                    }


                    Rectangle(backbuffDC, temp_shape.left, temp_shape.top, temp_shape.right, temp_shape.bottom);
                }
-2
votes

Your code is unnecessarily complex with temp DCs and back buffers and you are recreating GDI brushes in every WM_PAIT? But that's besides the point. Here is the question : why do you do this:

temp_shape.right=y1+(x2-x1);  //basing horizontal coordinate on vertical?

What framework stack is this on top of? .NET, then why don't you use built-in double buffering?