calculating pixel size from ppi - css

I need some help to verify that i have made my calculations correct.
I want to determine number of pixels in the span of 13.6 millimeters for a specific device that is 224,17 pixels per inch.
( if 1 inch = 224.17ppi therefor: 1 centimeter = 569.39ppcm therefor: 1 millimeter = 5.69 ppmm )
I calculate 13.6mm = 77.35 pixels

(224.17 pixels / 1 inch) * (1 inch / 25.4 mm) * 13.6 mm = 120 pixels

you can user converter for your ease like
http://endmemo.com/sconvert/millimeterpixel.php

Related

Analog to Digital input scaling equation works in codeblocks but not on Microcontroller

I'm so lost on how to fix this, it should be so simple. I'm using a pic16F1526 and trying to scale the analog to digital reading from 0-255 to 50-100 roughly. I am using this equation
result = ((user_input + 200) * 200) / 800;
In code blocks and on my calculator it works at all numbers from 0-255 and it works perfectly whether I use 8 bit, 16 bit variables in code bloacks.
I've already verified that the AtoD input is working correctly sending the data to the UART. Even if I enter static numbers in place of the sample I get weird results.
When the acd reads a 255 or I enter a 255 the equation gives me a 31 in decimal instead of 100 like it's supposed to. The only thing I can think of is something is getting messed up in the way an 8 bit PIC does it's math since it's an a bit micro.
Sounds like you are getting the correct results on in codeblack because of integer promotion and getting the incorrect results in the hardware because of variable overflow.
uint8_t Can contain 0 to 255
int8_t Can contain -126 125
uint16_t Can contain 0 to 65635
...
Assuming you have uint16_t, the micro's math will go as follows:
((255 + 200) * 200) / 800
(455 * 200) / 800 : 455 * 200 Overflows the 16 bit variable!
( 25464 ) / 800: Note that 91000 & 0xFFFF == 25464
31
You can work around this issue by simplifying your equation :
(user_input + 200) / 4 is equivalent to ((user_input + 200) * 200) / 800 and will not overflow at 16 bits although your accuracy is not very high as ImaginaryHuman072889 pointed out.
If I understand your question correctly, you want to linearly map the numbers 0-255 to the numbers 50-100.
Back to good old y = mx + b algebra.
When x = 0, y = 50. Therefore:
y = mx + b
50 = m*0 + b
b = 50
When x = 255, y = 100. Therefore:
y = mx + 50
100 = m*255 +50
m*255 = 50
m = 50/255 = 10/51
Therefore, the precise answer is:
y = (10/51)*x + 50
On a side note, I have no idea how you got the result of 31 when plugging in 100 into your formula. See below.
(255+200)*200/800 = 113.75

How to calculate coordinates of the gunpoint

Good whatever time of day! I have some sprite:
A point has coordinates Actor.x; Actor.y.
AB length = 96
BC length = 86
AC length = 42
All calculations are approximate, I made it with help the ruler in Photoshop.
Sprite always towards mouse, angle (in radians) stores in Actor.direction variable. I draw sprite with scale 0.3.
All bullets toward mouse (that is Bullet.direction == Actor.direction). I need to create bullets in the B point. How I can calculate coordinates of the B point with any angle?
UPD
If I will create bullets in the coordinates:
x = Actor.x + 96 * math.cos(Actor.direction) * 0.3
y = Actor.y + 96 * math.sin(Actor.direction) * 0.3
I get it:
Excuse my bad English! It isn't my native language. Thank you in advance!
Let
cs = math.cos(Actor.direction)
sn = math.sin(Actor.direction)
point B will be shifted from A by
dx = - 42 * sn + 86 * cs
dy = 42 * cs + 86 * sn
Perhaps you will need to change signs before both 42s
(I did not account for scale)

Mathmatics to balance "multiple circuits"

I am building a game which allows the player to control "power flow" between 10 circuits.
Each of the 10 circuits is adjusted individually and the total must always equal 100%.
For example a perfectly balanced situation would be all 10 circuits at 10% (10x10=100)
Edit 2: If what I am trying to do here is know as things other than "balancing", please comment and I will research them.
Now the player also has the ability to lock circuits so that the power level cannot be changed by other circuits but it can still be changed directly.
EDIT 3: Sometimes the requested amount may not be possible to achieve (eg: example 3 and 6) in these situations the nearest possible result will be the result
EDIT: Seeing that my post is receiving down votes I will include what I have already tried
Sum of change divided by circuits requesting change adding to circuits requesting change and taken off circuits not changing - The problem with this method was negative and positive changes at the same time could balance and result in "deadlock" situations where no change happens
Looping circuit by circuit adding and taking as needed - The problem with this method is that it rarely balanced correctly
Applying subtractions and additions first and then balance all circuits back into range (so total becomes 100) - the problem with this was power would end where it shouldn't be with circuits that should be at 0 ending up with small amounts of power
To simplify my question we can work with just 5 circuits.
I need assistance to work out the math for calculating the following. After 20 or so attempts I am thinking I am over complicating it as I keep ending up with 200 line scripts or is this actually very complicated?
Example 1: Addition Example
20 20 20 20 20 Start values
+10 +10 0 0 0 Change
30 30 3.3 3.3 3.3 After first iteration
50 50 0 0 0 After x iterations (eg key held down)
Example 2: Subtraction Example
20 20 20 20 20 Start values
-10 -10 0 0 0 Change
10 10 26.6 26.6 26.6 After first iteration
0 0 33.3 33.3 33.3 After x iterations (eg key held down)
Example 3: Lock + Addition (L is locked)
L
2.5 90 2.5 2.5 2.5 Start values
0 0 +50 0 0 Change
0 90 10 0 0 After first iteration
0 90 10 0 0 After x iterations (eg key held down)
Example 4: Lock + Subtraction (L is locked)
L
2.5 90 2.5 2.5 2.5 Start values
0 -10 0 0 0 Change
5 80 5 5 5 After first iteration
25 0 25 25 25 After x iterations (eg key held down)
Example 5: Multi Lock + Subtraction (L is locked)
L L
2.5 90 2.5 2.5 2.5 Start values
0 -10 0 0 0 Change
5.8 80 2.5 5.8 5.8 After first iteration
32.5 0 2.5 32.5 32.5 After x iterations (eg key held down)
Example 6: Balancing change from unbalanced start (This math may be a bit off)
2.5 90 2.5 2.5 2.5 Start values
+10 +10 +10 0 0 Change
16.7 66.6 16.7 0 0 After first iteration
33.3 33.3 33.3 0 0 After x iterations (eg key held down)
Start by retrieving all circuits that may potentially be changed by the runtime:
Candidates = AllCircuits \ (LockedCircuits u ChangedCircuits)
Here, \ denotes the set minus operator and u is the union operator.
Calculate the average change per circuit:
targetTotalChange = totalChange
averageChange = totalChange / |Candidates|
Now, start to change the candidates. To account for limitations, order the candidates by their current power flow. If averageChange is negative, then order them in ascending order. If it is positive, order them in descending order.
And remember how many circuits you already have processed:
processedCircuits = 0
Now iterate all candidates in the specified order:
for each candidate in Candidates
Check if the the average change can be added to this circuit. Otherwise, adapt the values:
processedCircuits++
prevPower = candidate.PowerFlow
targetPower = prevPower + averageChange
if(targetPower < 0)
{
totalChange += prevPower
candidate.PowerFlow = 0
//recalculate average change
}
else if(targetPower > 100)
{
totalChange -= 100 - prevPower
candidate.PowerFlow = 100
//recalculate average change
}
else
{
totalChange -= averageChange
candidate.PowerFlow += averageChange
}
When you need to recalculate the average change, do the following:
averageChange = totalChange / (|Candidates| - processedCircuits)
Beware of division by zero.
Now you have adapted all other circuits. What remains is adapting the changed circuits. This is quite easy. We changed all other circuits by targetTotalChange - totalChange. This change can be added to the changed circuits. We can just add the according percentage:
percentage = (targetTotalChange - totalChange) / targetTotalChange
for each circuit in ChangedCircuits
circuit.PowerFlow += percentage * targetChange[circuit]
next

Calculate Height for Image/Video based on Aspect Ratio and Width

I have a width: 240
I have aspect ratio: 2.40
I need to get the height based on those two variables. What's the formula?
Definition of the ratio:
ratio = width / height
Formula:
height = width / ratio = 240 / 2.40 = 100
example of calculating 2:40 . using javascript programming language.
var width = 240 ;
var rat1 = 2;
var rat2 = 40;
var ratio = width / rat1;
var calculated_height = ratio * rat2;

Image scaling geometry

This is very programming related but a somewhat non-programming question. I am performing image scaling in a web based application and I need to maintain my image relative to a fixed location even though it scales anchored by its top, left corner. Hope the graphic make this possible.
The idea is that C is a fixed location that I want to maintain as my scaling origin rather than B which which is the current css behavior. C may or may not be within the actual image. So as the image scale, B needs to move relative to C. Example: if the image was scaled 50%, then B would move 1/2 the distance to C. If the image grew to 200% of its size, then B would move twice the distance away from C.
Ultimately looking for a formula for x & y for B given the location of C and a scaling factor for the image. Not sure the size of the image needs to be part of this but I have it if needed.
Thanks for any help!
Things I know:
I know the width and height of the
image rectangle.
I know the offset of B from A.
I know the offset of C from A.
I know the scale factor in percent of the image.
Effectively, you want to treat C as the origin, and just "move" B by the scaling amount. By treating it as a vector from C to B, and scaling it by the amount in question, you can do this fairly easily.
newBx = Cx - (Cx - Bx) * scale;
newBy = Cy - (Cy - By) * scale;
For example, with a scale of 0.5 (50%), this becomes:
newBx = 100 - (100 - 50) * 0.5
= 100 - 25
= 75 // 1/2 the distance to C
newBy = 100 - (100 - 25) * 0.5
= 100 - 37.5
= 62.5 // 1/2 the distance to C
With a scale of 2 (200%):
newBx = 100 - (100 - 50) * 2
= 100 - 100
= 0 // 2x the distance to C
newBy = 100 - (100 - 25) * 2
= 100 - 150
= -50 // 2x the distance to C
First you need to calculate the distance from B to C, then you just change that to scale, and that is where the new B is relative to C:
newB = C - (C - B) * scale
As you want the coordinates, it's the same function for x and y:
newBx = Cx - (Cx - Bx) * scale
newBy = Cy - (Cy - By) * scale
(The scale value used is not percentage but a size multiplier. An increase in size by 50% gives a scale of 1.5.)
So you want point C in the image which is currently at (C_x, C_y) to remain at the same position after scaling the image by a factor of s?
New position of C, say, C_new = (s*C_x,s*C_y).
And you want to move the image so that C_new = C.
Which means you'll have to shift B = (B_x,B_y) by (s*C_x-C_x,s*C_y-C_y), or the new origin of the image, say B_new is:
B_new = (B_x + s*C_x-C_x, B_y + s*C_y-C_y)
So now you can display the scaled image at B_new --- and C should remain fixed.
If I understand the problem:
X(b) = X(c) - Width*(1/3)
Y(b) = Y(c) - Height*(3/4)
The formula seems simple enough, but your sample image can't get any larger than 133x200 (scale = 133%) before it overruns Y=0 (which I assume is your northern limit).
If you want to stop it from moving past Y=0 or X=0, and push-out further to the south and east once it reaches either limit, one approach might be:
IIF(Height > 133, Y(b) = 0, Y(b) = Y(c) - Height*(3/4))
IIF(Width > 450, X(b) = 0, X(b) = X(c) - Width*(1/3))
I think scale should be converted to height and width, instead of using scale as a variable in these formulas, since your original image could be any size (assuming they're not always going to be 100x150 per your sample)
dave
here's a C# snippet that is tested to work:
void Main()
{
Application.Run(new form1());
}
public class form1 : Form
{
static Point C = new Point(100,100);
static Point origLocB = new Point(50,25);
static Size origSizeB = new Size(150,100);
Panel Rec = new Panel()
{
Left = origLocB.X,
Top = origLocB.Y,
Width = origSizeB.Width,
Height = origSizeB.Height,
BorderStyle = BorderStyle.FixedSingle,
};
NumericUpDown nud = new NumericUpDown()
{
Value = 1M,
Increment = .01M,
DecimalPlaces = 2,
Dock = DockStyle.Bottom,
};
public form1()
{
nud.ValueChanged += NumericUpDown_ValueChanged;
Controls.Add(nud);
Controls.Add(Rec);
}
public void NumericUpDown_ValueChanged(object sender, EventArgs e)
{
Rec.Location = new Point(((int)((origLocB.X - C.X) * nud.Value + C.X)),
((int)((origLocB.Y - C.Y) * nud.Value + C.Y)));
Rec.Size = new Size((int)(origSizeB.Width*nud.Value),
(int)(origSizeB.Height*nud.Value));
}
}
it really just echo's #Reed's Answer

Resources