views:

22

answers:

1

I'd like to know, how coordinates can be transformed to center of the form for drawing mathematical functions. I already tried ->TranslateTransform(x,y) on Graphics object, this works, but only in one quarter of coordinates. How should I draw math functions on the form?Programming C++ long, but WinForms and Drawing are new 4 me.

A: 

Very unclear what "quarter of coordinates" might mean. To get a Cartesian coordinate system with 0,0 in the center of the form and negative coordinates mapped to the lower left corner of the form or control, you will have to use ScaleTransform() to invert the Y-axis and TranslateTransform() to shift the origin to the center. Like this:

protected:
    virtual void OnPaint(PaintEventArgs^ e) override {
        e->Graphics->ScaleTransform(1, -1);
        e->Graphics->TranslateTransform(this->ClientSize.Width / 2, -this->ClientSize.Height / 2);
        e->Graphics->DrawLine(Pens::Black, -20, -20, 20, 20);
        __super::OnPaint(e);
    }

This draws the line from lower-left to upper-right.

Hans Passant
thank you! this was simpler than i imagine."quarter of coordinates" means quadrant of a Cartesian coordinate system,sorry for bad English. And will it show graph taking place in all four quadrants properly?(like sin(x));
Steel