c# - Flip coordinates when drawing set of rectangles -
i trying draw set of squares origin(0,0) of squares in top left corner , wanted squares render bottom right corner, , found code flip co ordinates, nothing rendering on winform.
i know have gone wrong in height attribute of translatetransform. dont why height required trying draw set of 2d squares.
i tried hardcoding height attribute still of no use.
public void scaletransformfloat(painteventargs e,list<square> lstsquare) { int height = 10; // begin graphics container graphicscontainer containerstate = e.graphics.begincontainer(); // flip y-axis e.graphics.scaletransform(1.0f, -1.0f); // translate drawing area accordingly // e.graphics.translatetransform(0.0f, -(float)height); // whatever draw (using graphics context) appear // though (0,0) @ bottom left corner //user-defined function draw square drawsquare(e,lstsquare); // end graphics container e.graphics.endcontainer(containerstate); }
method draw set of squares
public void drawsquare(painteventargs e,list<square> lstsquare) { foreach(square s in lstsquare){ e.graphics.drawrectangle(pens.black, 0,0 ,s.m_side, s.m_side); } }
when doing matrix operation transformations order matter.
in case need either translate first, scale/flip or reverse direction of translation. code seems right.
the height
not 3d height. total height
of target control want draw on.
imagine laying sheet of paper on control; control viewport. imagine flipping paper topwise top left edge. has left viewport. need move down. distance? answer: paper's height..
your code has height = 10
pixels. not size of control, right? maybe comes code copied; there meant form's height
. if want draw on form (usually not idea), delete line int height = 10;
!
let's simple example: draw few rectangles onto panel
:
private void panel1_paint(object sender, painteventargs e) { if (checkbox1.checked) { e.graphics.scaletransform(1, -1); e.graphics.translatetransform(0, -panel1.height); } e.graphics.drawrectangle(pens.violet, 1, 1, 33, 33); e.graphics.drawrectangle(pens.orangered, 11, 11, 133, 55); e.graphics.drawrectangle(pens.magenta, 44, 11, 233, 77); e.graphics.drawrectangle(pens.olive, 55, 44, 33, 99); } private void checkbox1_checkedchanged(object sender, eventargs e) { panel1.invalidate(); }
after checking checkbox
result flipped:
also note drawsquare
draws squares @ same location (0,0)
. not sure if want..
final note: drawrectangle
has nasty habit of overdrawing botton , right sides 1 pixel. after changing first loaction (1,1)
(0,0)
(then) bottom side cut off. might consider translating graphics coodinates 1 pixel less..
Comments
Post a Comment