So I have to randomize an x position. But for some reason the middle is 0 and therefore I need to do negative max x and positive max x. It's a little hard to explain so here is the code:
let height = UInt32(self.view!.frame.height)
let nHeight = ((height - height) - height)
let randomXPosition = Int(arc4random_uniform(UInt32(nHeight)) + height)
This doesn't give me any errors but when I run it crashes. I need a way to randomize the value for negative max X and positive max X so that the object is randomized in the screen.
If you need to generate a random number between -x and x then just create a random number between 0 and 2x and subtract x. arc4random_uniform(x * 2) - x.
Related
I want to display some values inside a chart-like tool based on pixels.
Problem is that the left xAxis has a max scale of 200 pixel. Inside that pixel square i want to display different altitude values that can range from 200m-1500m or 324m-724m or anything else.
So i need to recalculate the orignal values by a factor to display them inside this chart. Haven't find the right solution yet. Any hints ?
You have range of Y-coordinates 0..YMax (200 for your case) and data range Data_Low..Data_High (find min and max values).
To map data range to axis range, use linear formula:
Y = (Value - Data_Low) * YMax / (Data_High - Data_Low)
If axis starts from YMin, use
Y = YMin + (Value - Data_Low) * (YMax - YMin) / (Data_High - Data_Low)
I want to truncate any value that appears after two decimal places in Matlab/Octave.
I do not want the value 1. I instead want the value to be 0.99 after "rounding/flooring".
Please use only built-in functions to accomplish this task.
Scale up the number, apply rounding, scale the result down by the same value:
x = 0.999;
y = floor (100 * x) / 100;
My goal is to create a graph similar to a picture below. I actually managed to implement it with combination of a magic numbers and set scale (0.001 - 1000). So to summarize I am looking for a formula that will calculate right position to plot lines on logarithmic y scale for range of predefined values.
Y axis: logarithmic scale
X axis: Integers
Any help will be welcome!
I solved it thanks to help of #DietrichEpp. Here is the function that calculates Y coordinates given:
screenY0 - min point on y axis
screenY1 - max point on y axis
dataY0 - value responding to the top of the scale
dataY1 - value responding to the bottom of the scale
func convert(data: Double, screenY0:CGFloat, screenY1:CGFloat, dataY0:Double, dataY1:CGFloat) ->CGFloat{
return screenY0 + (log(CGFloat(data)) - log(CGFloat(dataY0))) / (log(CGFloat(dataY1)) - log(CGFloat(dataY0))) * (screenY1 - screenY0)
}
I am implementing a rightclick context menu on my google v3 map and I need to get the pixel x and y to correctly position the menu. I get the lat and the lng, anyone have a nice solution to get the pixel x and y?
Best Regards
Henkemota
index=x+(y*height)
x = index % width
y = index / height
Correction to the above answer:
index=x+(y*width)
//(not y*height ... because you're taking one full horizontal line of pixels (e.g. 1280px) and multiplying that by the number of lines (y) down the screen at which x is, then adding x to account for x pixels over in the next full line.)
x = index % width
y = index / height
Example: I have a circle which is split up into two halfs. One half goes from 0 to -179,99999999999 while the other goes from 0 to 179,99999999999. Typical example: transform.rotation.z of an CALayer. Instead of reaching from 0 to 360 it is slip up like that.
So when I want to develop a gauge for example (in theory), I want to read values from 0 to 360 rather than getting a -142 and thinking about what that might be on that 0-360 scale.
How to convert this mathematically correctly? Sine? Cosine? Is there anything useful for this?
Isn't the normalization achieved by something as simple as:
assert(value >= -180.0 && value <= +180.0);
if (value < 0)
value += 360.0;
I'd probably put even this into a function if I'm going to need it in more than one place. If the code needs to deal with numbers that might already be normalized, then you change the assertion. If it needs to deal with numbers outside the range -180..+360, then you have more work to do (adding or subtracting appropriate multiples of 360).
while (x < 0) {
x = x + 360;
}
while (x > 360) {
x = x - 360;
}
This will work on any value, positive or negative.
((value % 360) + 360) % 360
The first (value % 360) makes it to -359 to 359.
The + 360 removes any negative number: Value now 1 to 719
The last % 360 makes it to 0
to 359
Say x is the value with range (-180, 180), y is the value you want display,
y = x + 180;
That will change shift reading to range (0, 360).
If you don't mind spending a few extra CPU cycles on values that are already positive, this should work on any value -360 < x < 360:
x = (x + 360) % 360;
I provide code to return 0 - 360 degree angle values from the layer's transform property in this answer to your previous question.