-2

With this code I want to draw a rectangle:

procedure TForm1.Button1Click(Sender: TObject);
var rectangle:Trect;
begin
  fx:=400;
  fy:=400;
  sc1:=base/fx;
  sc2:=altezza/fy;
  sc:=max(sc1, sc2);
  lx:=fx*sc;
  ly:=fy*sc;
  xc:=base/2;
  yc:=altezza/2;
  x1:=xc-(lx/2); x2:=xc+(lx/2); y1:=yc-(ly/2); y2:=yc+(ly/2);
  panel1.Repaint; 
  panel1.Canvas.Brush.color:= clblack;
  panel1.Canvas.line((panel1.width div 2),0,(panel1.Width div 2), panel1.Height);
  panel1.Canvas.line(0,(panel1.height div 2), panel1.Width,(panel1.Height div 2));
  panel1.canvas.brush.style:=bsclear;
  Rectangle:=rect(x1, y1, x2, y2);
end;  

But there is a problem because I have to use only integer values.

Is it possible to use real values for drawing a rectangle with TCanvas?

Johan
  • 74,508
  • 24
  • 191
  • 319
C P
  • 35
  • 1
  • 4
  • Transform between your logical coordinate system and the screen's coordinate system. – Andreas Rejbrand Oct 19 '13 at 15:00
  • tanks but...how do i do this? – C P Oct 19 '13 at 15:40
  • 1
    I've answered that question before at SO: http://stackoverflow.com/a/16778075/282848 – Andreas Rejbrand Oct 19 '13 at 15:45
  • 1
    It is very hard for us to guess what the variables in your code are supposed to represent, but perhaps you already think in terms of the screen's coordinate system, and only need to use the `Round` function: `Rect(Round(x1), Round(y1), Round(x2), Round(y2));` – Andreas Rejbrand Oct 19 '13 at 16:00
  • practically i want to draw a rectangle with dimension assigned from two Tedit in cm. For exemple 30*50cm, or 30.5*52.5 cm etc – C P Oct 19 '13 at 16:40
  • 2
    possible duplicate of [Draw image in canvas with decimal values](http://stackoverflow.com/questions/16777977/draw-image-in-canvas-with-decimal-values) – Johan Oct 19 '13 at 16:56
  • 1
    If you use `div` instead of `/` your divisions will produce integer values instead of floating point (provided the input is integer to begin with). Then you don't need to `round()`. On style: it's a very bad idea to use global variables like you do in your example. – Johan Oct 19 '13 at 16:58

1 Answers1

2

The simple answer is no. Graphic devices as represented by TCanvas use a coordinate system with integral coordinates. If your coordinates are real values then you need to use some form of mapping between your coordinate system and the integral device coordinates.

However, in this instance it looks like it's not that complex. You don't need real valued coordinates per se. You only have real values because you used real division. Perhaps all you need to do is use integer division, div, rather than real division. Or perhaps you would prefer Round.

A bigger problem is that your code is in the wrong place. You cannot paint in a button handler. Windows will not remember what you painted. The next time the window is invalidated it will ask the panel to refresh itself, and your rectangle will be gone. Painting code needs to be inside an overriden Paint method or equivalent. Perhaps you need a paint box control.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • It's still good practice to always keep the real values stored, used, calculated, and otherwise implemented in their original real values, and only scale them to integral values at the time of drawing. – Jerry Dodge Oct 19 '13 at 21:22
  • @JerryDodge Yes, that's the mapping. – David Heffernan Oct 19 '13 at 21:25
  • Just figured it should be emphasized that the only time to assume integral coordinates is at that moment of canvas drawing. – Jerry Dodge Oct 19 '13 at 21:27