Home > Net >  Windows API CRect Inverting so works like graph paper
Windows API CRect Inverting so works like graph paper

Time:03-20

I guess i am old school when it comes to doing graphics (graphs) etc. My Brain = Bottom/Left is 0,0, Top is Size y, Right is Size x DC Rect = Top/Left is 0,0, Bottom is Size y, Right is Size x

Hence I find drawing in Windows DC Rect a pain as Y is inverted and I have to flip my brain for each drawing function.

Is there a way to draw in Rect or DC in graph mode and/or flip/mirror it into the Windows dc rect?

CodePudding user response:

Hello Paddy and many thanks for your most enlightened and practical view of view. Why i ask about this flipping the vertical view is that i am developing an autonomous wheeled robot. The software for it I am developing in two parts, the Motor/Sensor part in ARDUINO STM32 which broadcasts motor speeds/distance to objects etc and the host/brain part that interprets the broadcast data and issues motor commands etc. Now the second part host/brain function is going to be in a PC that communicates with ERIN (The Robot) over a radio link. Hence I am developing a MFC graphics application that displays ERIN's journey. Currently ERIN is stationary at the bottom of the CRect looking upwards, hence what the sensor see is in relative negative CRect space and bit of a pain flipping my brain all the time as I think of it in positive space. Think maybe I'll rotate ERIN 90 degrees to starboard and have the space moving right (positive) to left toward ERIN. Thanks again for the input imk

CodePudding user response:

You can move the viewport origin anywhere you like. This is done using either SetViewportOrgEx (GDI) or CDC::SetViewportOrg in MFC.

The fundamentals are explained in Microsoft's documentation under Coordinate Spaces and Transformations.

While this moves the viewport origin to an arbitrary position, it doesn't flip the orientation along the y-axis. There are several ways to accomplish that, though the easiest is probably to take advantage of MFC's adjustable viewport scaling (CDC::ScaleViewportExt). The following OnPaint implementation draws a line from (0, 0) to (height, width). With the viewport adjustments in place, this line goes from the bottom left to the top right.

void CSampleDlg::OnPaint()
{
    CPaintDC dc{ this };

    CRect rc{};
    GetClientRect(&rc);
    auto const width{ rc.right - rc.left };
    auto const height{ rc.bottom - rc.top };

    dc.SetMapMode(MM_ANISOTROPIC);
    dc.SetViewportOrg(0, height);
    dc.ScaleViewportExt(1, 1, -1, 1);

    dc.MoveTo(0, 0);
    dc.LineTo(width, height);
}
  • Related