How accurate is using millimeters in CSS? - css

I'm trying to use mm in CSS as opposed to the standard px.
first is this even possible?
if so, can i use it like this:
#div{
width:200mm;
height:100mm;
}
I did try to convert the millimeters to pixels so I can use the pixels value but every converter returns a different value which is strange. even Photoshop converts it and gives me a different value.
Could someone please advise on this issue?

The CSS Values and Units Module Level 3 defines the mm unit as 1/10th of 1cm. It also defines the cm unit as 96px/2.54.
In CSS, 200mm and 100mm are always equal to the same px value:
For 200mm:
cm = (200mm / 10)
px = cm * (96 / 2.54)
-> 755.91px
For 100mm:
cm = (100mm / 10)
px = cm * (96 / 2.54)
-> 377.95px
(Note that these values are rounded).

Related

How to compare two Ratios of Rectangles / Sizes to determine their percentages of differences?

I have a ractangle whose ratio is 80:40 and other rectangles with similar aspect ratios within a humbral for example 80:35, 85:45, that unbral is a decimal or integer.
My problem is that I need to compare other rectangles with the first one and determine a difference in their respective aspect ratios in percentages, for example: 80:30 is 20% different in aspect ratio than 80:40.
(20% is not a calculated data, it is an example idea, because I don't know how to do it).
Could it be that a totally opposite aspect ratio is 100% different? For example: 80:40 is 100% different than 40:80
Imagine that you have a collection of rectangles and a target rectangle and you have to filter the collection leaving only those rectangles that have an aspect ratio similar to the target rectangle.
Sample:
private float GetDiffRatio(FloatSize size1, FloatSize size2) {
float fixedR1 = size1.Width / size1.Height;
if (fixedR1 >= 0)
// Invert and negative.
fixedR1 = -(size1.Height / size1.Width);
float fixedR2 = size2.Width / size2.Height;
if (fixedR2 >= 0)
// Invert and negative.
fixedR2 = -(size2.Height / size1.Width);
float rDiff = fixedR1 - fixedR2;
return rDiff * 100f;
}
Test:
float diffRatio = GetDiffRatio(new FloatSize(100f, 50f), new FloatSize(50f, 100f));
Results = -100f
Test2 (inverted order of parameters):
float diffRatio = GetDiffRatio(new FloatSize(50f, 100f), new FloatSize(100f, 50f));
Results = 100f
I am not sure that this is a valid or correct form, I do not know if it can generate any condition that returns a wrong percentage of similarity.
The answer to this question depends a lot on what exactly you're trying to do or you're planning to uses this similarity function for. I find it very unintuitive to say that opposite aspect ratio leads to 0% similarity. I think comparing two rectangles r1=(2.1,2) and r2=(2,2.1) should be a lot more similar to each other then for example r3=(1,5) r4=(5,1).
This is not me saying that it couldn't be useful in some case to have a similarity function like your's, I just want to explain that it depends a lot on what you're doing ...
I would say a very obvious solution would be to just divide width by height of every rectangle and take as similarity-function s1 the absolute value from both values subtracted. So for my provided examples the result would be:
s1(r1,r2) = | 2.1/2 - 2/2.1 | = 0.0976...
s1(r2,r3) = | 1/5 - 5/1 | = 4.8
If it is also important that you have values between 0 and 1 you could additionally plug this values in for example this function ...
where
b must be smaller than 0 and is a parameter with what you can controll how fast the funciton converges to 1. YOu can play around with it here: https://www.desmos.com/calculator/nwlq3ujouq
In the case you really want something as you suggested, i would simply do the following:
You take your constraint that every rectangle is between the ratio a:b and c:d. Than you calculate x1=a/b and x2=c/d and then you interpolate the value from zero to one between those values so:
h(x1) = 0
h(x2) = 1
if you need more details on how to do this look here https://en.wikipedia.org/wiki/Linear_interpolation but i think it's very straight forward.
The similarity function s3 can then be build again with the absolute value of the difference
s3=| h(r1)-h(r2) |

What exactly does this calculation mean?

I'm new to GLSL and learning from the tutorial here.
(It's using ShaderToy)
https://gamedevelopment.tutsplus.com/tutorials/a-beginners-guide-to-coding-graphics-shaders--cms-23313
My question is why you can set x coordinates to 0-1 by dividing the fragCoord's x coordinates by the iResolution(screensize).
It might be just a math question, but I'm confused what exactly the "iResolution.x" indicates or what kind of calculation is made here. (Is it a vector division? )
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 xy = fragCoord.xy; //We obtain our coordinates for the current pixel
xy.x = xy.x / iResolution.x; //We divide the coordinates by the screen size
xy.y = xy.y / iResolution.y;
// Now x is 0 for the leftmost pixel, and 1 for the rightmost pixel
vec4 solidRed = vec4(0,0.0,0.0,1.0); //This is actually black right now
if(xy.x > 0.5){
solidRed.r = 1.0; //Set its red component to 1.0
}
fragColor = solidRed;
}
The other answers are correct. fragCoord is the pixel currently being drawn, iResolution is the size of the screen so
xy.x = xy.x / iResolution.x; //We divide the coordinates by the screen size
xy.y = xy.y / iResolution.y
Gives normalized values where xy.x goes from 0 to 1 across and xy.y goes from 0 to 1 up the screen which seems to be exactly what the comments say
It's important to note though that iResolution and fragCoord are user variables. In this case I'm guessing you're getting this GLSL from Shadertoy. Those variables are not part of WebGL or GLSL, they are defined by Shadertoy and so their values and meaning are defined by shadertoy.
Note that if you are new to GLSL and WebGL you might want to consider some webgl tutorials. Also see this answer about shadertoy
iResolution.x is the width of your screen in pixels. Dividing the pixel x location by the total width transforms the location into a fraction of the screen width. So, if your screen is 1000 pixels wide, and your current position is x=500, xy.x = xy.x / iResolution.x; will convert xy.x to 0.500.

Calcluation of viewport coordinates

I read an article about normalized device coordinates (on the german DGL wiki) and the following example is provided:
"Let's consider that we had a Viewport with dimensions 1024 pixel(width) and 768 pixel height. A point P with absolute, not normalized, coordinates P(350/210) would be in normalized coordinates P(-0,32/-0,59).These coordinates can now be projected on a Viewport (800x600) just by multiplying the normalized device coordinates (similar to vector scaling) with the size of the viewport. In this case the result would be P(273/164).
Somehow I can't understand how one can get to the result provided (I mean 273/164 and -0,32/-0,59 ...could somebody explain to me how to calculate the coordinates?
P.S. : This is the article - https://wiki.delphigl.com/index.php/Normalisierte_Ger%C3%A4tekoordinate
Thank you!
That article is definitely lacking description. I can get you part of the way there; maybe someone with more math can help finish.
According to this answer, the formula to convert non-normalized coords to normalized coords is:
(where Cx/y = Coordinate X/Y; Sx/y = Screen X/Y; and Nx/y = Normalized X/Y).
Plugging the example's numbers in:
Nx = (350/1024) * 2 - 1 = -0.31640625
Ny = 1 - (210/768) * 2 = 0.453125
...or (-.36, 0.45).
Reversing this to get the new coords:
Cx = (1 + -0.31640625) / 2 * 800 = 273.4375
Cy = (1 - 0.453125) / 2 * 600 = 164.0625
Note that the Y value doesn't match. This is probably because my calculation doesn't account for the aspect ratio, and it should be since these screens have a .75 aspect ratio, while NDC's is 1. This SO answer may help too.

Computing target DPI value from specific dimensions

A rather basic maths problem.
I got an image with a specific width and height in pixels:
WIDTH = 3648 px
HEIGHT = 2736 px
In order to compute the target print size in millimeters, given a specific DPI amount (200) i came up with this:
PRINT-WIDTH = IMAGE-WIDTH-PX / 200 * 2.54 * 10;
PRINT-HEIGHT = IMAGE-HEIGHT-PX / 200 * 2.54 * 10;
This works well. In our example it computes
463 x 347 mm
as target print size. Perfect.
However, i now must be able to make changes to the widths and heights in millimeters, and based on the fact that we assume 200 DPI for printing, i must compute the new DPI value.
So for instance, when changing 463 x 347 to 400 x 300 i should somehow be able to calculate how that affects the DPI.
The only possible approach that came to my mind was to compute the difference between the old and the new format as a percentage, and then apply that percentage to the DPI. But the results are incorrect.
How can i compute the DPI value from the new width and height, given the original 200 DPI matching the original format?
NewDPI = 200 * 463 / 400
Or without using DPI 200 at all:
NeededDPI = IMAGE-WIDTH-PX(3648) * 25.4 / PRINT-WIDTH(400)

Math - Getting Maximum Proportionate Image Scale

I need an algorithm which will determine the maximum scale increase that is possible for an image.
The rules (size limitation of a BitmapData object for the Flash Player, but this is not a Flash question):
both the width and length of the image must be less than 8,191 pixels
the maximum number of pixels in the image can not exceed 16,777,215
therefore, as mentioned in the documentation:
if a BitmapData object is 8,191 pixels
wide, it can only be 2,048 pixels
high.
In my code, I first determine if any of these rules are broken and throw and error if they are. this lets me know that any image that is loaded and does not throw an error has scalability.
The image I'm using is 2514 width x 1029 height. This image will not throw an error since both width and height are less than 8,191 and it's pixel count, or width multiplied by height, is less than 16,777,215.
I identify as being right-brained and don't have much confidence in my math skills, but the following is what I've come up with to determine the maximum allowed scale for the image.
private static const MAX_BITMAP_MEASUREMENT:uint = 8191;
private static const MAX_BITMAP_PIXELS:uint = 16777215;
var imageWidth:uint = 2514;
var imageHeight:uint = 1029;
var roughScaleUp:Number = 1.0 / Math.max(imageWidth, imageHeight) * MAX_BITMAP_MEASUREMENT;
var scaleBack:Number = Math.max(Math.min(imageWidth, imageHeight) * roughScaleUp - MAX_BITMAP_PIXELS / MAX_BITMAP_MEASUREMENT, 0);
var maxScale:Number = 1.0 / (Math.max(imageWidth, imageHeight) + scaleBack) * MAX_BITMAP_MEASUREMENT;
This code outputs the maximum scale for my image as 2.145144435977516, but I tested it and there is still a lot of pixel space remaining, so it should be able to be scaled up more and I'm pretty sure my code is terribly wrong.
Any math wizards on here care to help out a lowly art school graduate? I'm fully ready to accept that there is probably a much more simplistic solution to this problem and I'm prepared for the lashings.
You have to multiply both the width and the height by a constant, and the scaled result of the multiplication should be less than 16777215.
So
a^2 * w * h == 16,777,215
Which yields, for your values of w and h
a = 2.5466520486244177 [= Sqrt(16,777,215 / (w*h)) ]
So , for your values for the new w and h, you get:
NewW = a * w = 6402.283250241786
NewH = a * h = 2620.5049580345258
... just round them down :)
Well, here is an ugly solution. I couldn't get the rounding quite right, so I brute forced over the 4 possibilities to get the optimal value. The code should be straightforward enough to understand:
from math import *
def opt_image(w, h):
aspect = w / h
if aspect >= 1:
v = min(sqrt(1677215 / aspect), 8191)
a, b = floor(aspect * v), floor(v)
area, nw, nh = max([ ( (a+x)*(b+y), (a+x), (b+y) ) for x in range(2) for y in range(2) if (a+x)*(b+y) < 1677215 ])
return nw, nh
a, b = opt_image(w, h)
return b, a
For your example with width 2514, height 1029; I got:
(1831.0, 916.0)

Resources