### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: How to draw in real size to screen and printer  (Read 399 times)

#### AL

• Full Member
• Posts: 209
##### How to draw in real size to screen and printer
« on: November 04, 2020, 10:20:01 pm »
I am doing my first attempt at graphics and need help.
I want to draw a simple shape to the screen with actual size and eventually print it so I can get a template.
I am using a paintbox and I can show a line so this is fine. But... whatever I do to calculate the line lenght it always come 2-3 mm longer that it is supposed (when I measure on the screen).

I calculate the number of pixels with : trunc( length in mm /25.4 * ScreenDPI)

Do I miss something?

Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2.
3. var
4.   w, h: Integer;    // Width and height of the rectangle
5.   cx, cy: Integer;  // center of the form
6.   lx: integer ; // left x coord
7.   R: TRect;         // record containing the coordinates of the rectangle's left, top, right, bottom corners
8.   ScrnDPI, NutPixel : Integer ;
9.   NutWidth : real ;
10. begin
11.    NutWidth := 50 ;
12.    ScrnDPI := Screen.PixelsPerInch ;
13.    memo1.Append('ScreenDPI ' + ScrnDPI.ToString);
14.
15.    NutPixel := trunc(NutWidth/25.4 * ScrnDPI) ;
16.    memo1.Append('NutPixel ' + NutPixel.ToString);
17.
18.    // Calculate PaintBox center
19.
20.   cx := NutPaintBox.Width div 2;
21.   cy := NutPaintBox.Height div 2;
22.
23.   // Calculate the size of the rectangle
24.   w := NutPaintBox.Width ;//div 2;
25.   h := NutPaintBox.Height;// div 2;
26.
27.   // Calculate the corner points of the rectangle
28.   R.Left := cx - w div 2;
29.   R.Top := cy - h div 2;
30.   R.Right := cx + w div 2;
31.   R.Bottom := cy + h div 2;
32.
33.  with NutPaintBox do begin
34.   // Set the fill color
35.   Canvas.Brush.Color := clgray;
36.   Canvas.Brush.Style := bsSolid;
37.
38.   // Set the border color
39.   Canvas.Pen.Color := clBlue;
40.   Canvas.Pen.Width := 2;
41.   Canvas.Pen.Style := psSolid;
42.
43.   // Draw the rectangle
44.   Canvas.Rectangle(R);
45.
46.   // Draw a line
47.   Canvas.Pen.Width := 1;
48.   // left coordinate to have a centered line
49.   lx :=  (cx - NutPixel div 2) ;
50.   memo1.Append('lx ' + lx.ToString);
51.
52.   Canvas.Line( lx,cy,(lx + NutPixel),cy);
53.
54. end;
55. end;
56.
Laz 2.1.0, fpc 3.3.1, Win10
Laz 2.1.0, fpc 3.3.1, Mac OS Mojave running on VMWare
Laz 2.1.0  fpc 3.3.1 Ubuntu 20.04

#### winni

• Hero Member
• Posts: 2009
##### Re: How to draw in real size to screen and printer
« Reply #1 on: November 04, 2020, 10:40:31 pm »
Hi!

If you work with integers, statistically on every second DIV you loose one pixel.

Use the BGRAbitmap which can calculate subpixels with single values.

BGRAbitmap is available in the Online Packet Manager.

Winni

#### AL

• Full Member
• Posts: 209
##### Re: How to draw in real size to screen and printer
« Reply #2 on: November 04, 2020, 10:48:10 pm »
Thank you, I will look in that.
However, I do not loose pixels, my line is longer so I have more!
Laz 2.1.0, fpc 3.3.1, Win10
Laz 2.1.0, fpc 3.3.1, Mac OS Mojave running on VMWare
Laz 2.1.0  fpc 3.3.1 Ubuntu 20.04

#### howardpc

• Hero Member
• Posts: 3584
##### Re: How to draw in real size to screen and printer
« Reply #3 on: November 04, 2020, 10:50:19 pm »
Additionally, the values reported for your screen's X/Y pixels per inch ratios -- depending on your actual hardware and its drivers -- may be approximate rather than exact; and in any case are defined as integral quantities with no provision for sub-pixels. On large screens this gives quite a margin of error.

Hence why some CAD and other design software that needs high precision for screen representation may get each user to run a calibration routine when the program is first used to customise it for the actual hardware it is running on.

#### VTwin

• Hero Member
• Posts: 938
• Former Turbo Pascal 3 user
##### Re: How to draw in real size to screen and printer
« Reply #4 on: November 05, 2020, 12:00:52 am »
Measurements on the screen are likely to be variable. When you actually save it to a jpeg, png, or tiff, you should be able to set the dpi and get very good results when printing.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.0
macOS 10.13.6: Lazarus 2.0.10 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.10 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.10 (64 bit on VBox)

#### winni

• Hero Member
• Posts: 2009
##### Re: How to draw in real size to screen and printer
« Reply #5 on: November 05, 2020, 01:02:46 am »
Hi!

China???

1 metre is equal to 30 chinese inch, or 39.370078740157 inches.

From:

https://www.convertunits.com/from/chinese+inch/to/inches

Winni

PS.: I think you should care more about howardpc and VTwin answers

#### circular

• Hero Member
• Posts: 3573
##### Re: How to draw in real size to screen and printer
« Reply #6 on: November 05, 2020, 05:21:39 am »
Screen DPI values can even be completely off. Their initial value is always 96 whatever the screen and when you make elements bigger in Windows or Linux, you are freely changing the value of the DPI.

If you want to know, you need to calibrate it: draw a line that is supposed to be 10 cm, ask the person to measure it and from there determine the real DPI.
Conscience is the debugger of the mind