can we translate the coordinate when drawing in a paintbox lazarus / 39-Graphics ---------------------------------------------------------------------- vkhoa · 2014-06-18 10:05 can we translate the coordinate or rotate it? ---------------------------------------------------------------------- Blaazen · 2014-06-18 10:22 Coordinate of what? ---------------------------------------------------------------------- vkhoa · 2014-06-18 12:17 sorry for the question is not clear, it's the coordinate of a canvas of a paintbox I want to paint in the relative coordinate ---------------------------------------------------------------------- Blaazen · 2014-06-18 12:32 You probably mean one of these functions: Code: [Select]  ControlToScreen();   ScreenToControl();   ClientToScreen();   ScreenToClient();   ClientToParent();   ParentToClient(); For example Code: [Select]procedure TForm1.Button1Click(Sender: TObject); var aPoint: TPoint; begin   aPoint:=Button1.ControlToScreen(Point(0, 0));   Writeln(aPoint.X, ' ', aPoint.Y); end;will write absolute coordinates of Button1 (from Top-Left corner of the screen). ---------------------------------------------------------------------- vkhoa · 2014-06-19 00:24 no, that's not what I mean, the coordinate I say is in the canvas , the top left corner of paintbox is(0,0), now I want the (0,0) is at (50,50) what I want is something like this Code: [Select]paintbox1.canvas.line(0,0,200,100);//this line will draw a line from 0,0 to 200,100 translate(50,50);                            //is there some method like this?it translate the coorddinate by 50,50 paintbox1.canvas.line(0,0,100,60);//now this line will draw a line from 50,50 to 150,110 ---------------------------------------------------------------------- taazz · 2014-06-19 00:44 no but it is easy to write something like this. there is a proof of concept canvas in my simplegraph port to lazarus at http://sourceforge.net/projects/evssimplegraph/ unit usimplegraph it has 2 canvas based implementations one based on TCustomCanvas and one based on TControlCanvas they are both there because I experiment with different ways to replace the control's default canvas, the one used in the component it self is the TEvsGraphCanvas which is based on the TcontrolCanvas, you can look on the TEvsSimpleGraph constructor for the code used to replace the existing TControlCanvas. Be warned only the translation is active no zoom or rotation is supported it requires a bit of work to add those two. ---------------------------------------------------------------------- engkin · 2014-06-19 02:43 Quote from: vkhoa on June 19, 2014, 02:24:20 amno, that's not what I mean, the coordinate I say is in the canvas , the top left corner of paintbox is(0,0), now I want the (0,0) is at (50,50) what I want is something like this Code: [Select]paintbox1.canvas.line(0,0,200,100);//this line will draw a line from 0,0 to 200,100 translate(50,50);                            //is there some method like this?it translate the coorddinate by 50,50 paintbox1.canvas.line(0,0,100,60);//now this line will draw a line from 50,50 to 150,110Windows supports it: Code: [Select]uses   .., Windows; ... var   xf:XFORM; begin   //Enable world transformation   SetGraphicsMode(Canvas.Handle, GM_ADVANCED);   //Prepare some transformation structure   //based on:   //x' = x * eM11 + y * eM21 + eDx,   //y' = x * eM12 + y * eM22 + eDy,   xf.eM11:=1;   xf.eM22:=1;   xf.eDx:=50;   xf.eDy:=50;   //before applying transformation   Canvas.Line(0,0,200,100);   //apply wanted transformation   SetWorldTransform(Canvas.Handle, xf);   Canvas.Line(0,0,100,60); end; ---------------------------------------------------------------------- vkhoa · 2014-06-19 08:29 thanks for both replies I have tried engkin example, it worked but... I was curious about the default value of the properties em12,em21 (thinking it is equal to 0 when I guess em11,em22 is the scale rate) so I coded this Code: [Select]Procedure TForm1.PaintBox1Paint(Sender: TObject); var xf:xform; Begin   //Enable world transformation    SetGraphicsMode(paintbox1.Canvas.Handle, GM_ADVANCED);    //Prepare some transformation structure   //based on:   //x' = x * eM11 + y * eM21 + eDx,   //y' = x * eM12 + y * eM22 + eDy,   xf.eM11:=3;   xf.eM22:=1;   xf.eDx:=50;   xf.eDy:=50;   //before applying transformation   paintbox1.Canvas.Line(0,0,200,100);   paintbox1.Canvas.TextOut(0,0,floattostr(xf.eM12)+', '+floattostr(xf.eM21));   //apply wanted transformation   SetWorldTransform(paintbox1.Canvas.Handle, xf);   paintbox1.Canvas.Line(0,0,100,60);   paintbox1.Canvas.Font.Color:=clblue;   paintbox1.Canvas.TextOut(0,0,floattostr(xf.eM12)+', '+floattostr(xf.eM21)); end;  the first textout it displayed corectly, but the second textout(after setworldtransform) it displayed nothing,i also changed the text parameters to a constance string but it still did not display, is it an issue and another question, why em12,em21 is a very small positive value but not equal to 0 ---------------------------------------------------------------------- engkin · 2014-06-19 12:21 Quote from: vkhoa on June 19, 2014, 10:29:56 amI was curious about the default value of the properties em12,em21 (thinking it is equal to 0 when I guess em11,em22 is the scale rate) .. why em12,em21 is a very small positive value but not equal to 0 Global variables are initialized to zero by default (and they reside on the heap). Local function variables are *not* initialized and hold random values (they reside on the stack) and you need to initialize them. Add the following line to the beginning of your code: Code: [Select]Procedure TForm1.PaintBox1Paint(Sender: TObject); var   xf:xform; Begin   ZeroMemory(@xf, SizeOf(xf));  //<------ This line   //Enable world transformation    SetGraphicsMode(paintbox1.Canvas.Handle, GM_ADVANCED); ... ---------------------------------------------------------------------- vkhoa · 2014-06-20 09:54 thanks, I got it.