3

I want to create a sketch as given in the picture;

enter image description here

Is it possible to create A4 size sketch for certain screen size and resolution. If so how can I calculate values for this. Because as you know processing uses size() method for that and values for size is pixels not cm or mm.

Thus, I want to draw 8 cm lines and whenever user clicks on some point on line processing will show distance to center point (red dot) in cm. I can do these things in pixel wise.However I don't how can I convert these values to cm or mm.

JamesFrost
  • 729
  • 11
  • 20
Can
  • 369
  • 4
  • 16

1 Answers1

3

This code appears to work (returns dpi):

import java.awt.Toolkit;
int resolution = Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println(resolution);

However, this answer suggests it is unreliable. If you only want to run it on a single monitor, then you could calculate it manually:

processing width = horizontal resolution(#pixels) / screen width(cm) * page width(cm)
processing height = vertical resolution(#pixels) / screen height(cm) * page height(cm)

EDIT: Here's a function to convert a distance in centimeters to # of pixels:

int cmToPixels(float cm){
  // this is a constant based on your screen's dimensions
  // my monitor is 1366x768
  int horizontalScreenResolution = 1366;
  // this is the actual width of your screen in cm
  int screenWidth = 32;
  // NOTE: because pixels/cm should be the same vertically and horizontally,
  // you could do the vertical equivalents for the above two constants.
  return cm * horizontalScreenResolution / screenWidth;
}

The idea is that you divide the total number of pixels by the distance they take up, giving you pixels per cm. Then, you multiply by the number of cm that you want your line to be, and it gives you the pixels to make the line.

Community
  • 1
  • 1
kevinsa5
  • 3,301
  • 2
  • 25
  • 28
  • Dear Kevin; Thank you so much for your answer. However, I don't know how I will reflect this measurements item's inside the sketch. For instance, how can I draw a line in 8 cm or how can I display distance between mouse and red dot in cm? – Can Feb 17 '14 at 23:17
  • I edited my answer with an example function. I hope that helps! – kevinsa5 Feb 17 '14 at 23:29
  • Dear Kevin; Thanks a million for your help.I used this method that wrote and it is working fine.However, there is one point that I did not understand. When I work with longitude( height) also this method works how is that possible because I see you make your calculations according to axis.Could you explain that to me please? – Can Feb 24 '14 at 16:18
  • For instance, I try to made 5 cm vertical gap using method written by you.It worked!. However, in your implementation you only calculate for horizontal value of monitor. – Can Feb 24 '14 at 16:20
  • The pixels in the screen should be a square grid, such that if N pixels fit into L centimeters vertically, the same amount should fit into the same distance horizontally. Another way to say that is that each pixel's height is the same as its width (in centimeters). Dividing the width of the screen by the number of pixels horizontally gives you the width of a single pixel (if you have 100 pixels covering 10 cm, each pixel is 10/100 = 0.1 cm). Since the width is the same as the height, you can use the same conversion. – kevinsa5 Feb 24 '14 at 16:23