Can
Can

Reputation: 369

processing drawings with real measurement values

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.

Upvotes: 3

Views: 568

Answers (1)

kevinsa5
kevinsa5

Reputation: 3411

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.

Upvotes: 3

Related Questions