Generating a random Gaussian double in Objective-C/C - objective-c

I'm trying to generate a random Gaussian double in Objective-C (the same as random.nextGaussian in Java). However rand_gauss() doesn't seem to work. Anyone know a way of achieving this?

This link shows how to calculate it using the standard random() function.
I should note that you'll likely have to make the ranf() routine that converts the output of random() from [0,MAX_INT] to be from [0,1], but that shouldn't be too difficult.
From the linked article:
The polar form of the Box-Muller transformation is both faster and more robust numerically. The algorithmic description of it is:
float x1, x2, w, y1, y2;
do {
x1 = 2.0 * ranf() - 1.0;
x2 = 2.0 * ranf() - 1.0;
w = x1 * x1 + x2 * x2;
} while ( w >= 1.0 );
w = sqrt( (-2.0 * ln( w ) ) / w );
y1 = x1 * w;
y2 = x2 * w;

Related

How can I assign a variable with the value of another variable in Kotlin?

In Code A, the parameters x1 and x2 use the same vaule, it works well.
I think I can improve Code A, so I write Code B, but it failed.
How can I assign x2 with the value of x1 ?
Code A
val stepWidth = step * index
it.drawChildnAxis(
x1 = stepWidth.toX, y1 = 0f.toY,
x2 = stepWidth.toX, y2 = yAxisLength.toY
)
fun Canvas.drawChildnAxis(x1:Float, y1:Float,x2:Float,y2:Float){
drawLine(
Offset(x = x1, y = y1),
Offset(x = x2, y = y2),
paintTableAxisChild
)
}
Code B
it.drawChildnAxis(
x1 = step * index.toX, y1 = 0f.toY,
x2 = x1, y2 = yAxisLength.toY
)
//The same
The x1 = ..., x2 = ... etc in your code are not actually assignment statements! They are named arguments.
There is no variable x1, x2 etc that becomes suddenly in scope at the function call, allowing you to assign values to it. This is just a bit of a syntax that lets you say the names of your parameters to make your code more readable, and sometimes resolve overload resolution ambiguities.
The syntax just so happened to be designed to look similar to assignments, making the left hand side look like a new variable just got declared. Would you still have this confusion if the syntax used : instead of =?
it.drawChildnAxis(
x1: stepWidth.toX, y1: 0f.toY,
x2: stepWidth.toX, y2: yAxisLength.toY
)
So x2 = x1 doesn't make sense - there is no such variable as x1 at that position. x1 is only the name of a parameter, which is only in scope when you are inside drawChildnAxis.
If you want to avoid repetition, just create a new variable yourself!
val x = stepWidth.toX
it.drawChildnAxis(
x1 = x, y1 = 0f.toY,
x2 = x, y2 = yAxisLength.toY
)
If you don't want x to be accessible afterwards, use a scope function:
stepWidth.toX.let { x ->
it.drawChildnAxis(
x1 = x, y1 = 0f.toY,
x2 = x, y2 = yAxisLength.toY
)
}
All of this is of course assuming that toX doesn't have side effects - calling its getter twice on the same thing gives the same value.

Why is my naive line drawing algorithm faster than Bresenham

I implemented both the naive line drawing algorithm and bresenham algorithm. When I run the program with a 1000 lines, the naive line drawing algorithm is faster than the bresenham algorithm. Could anyone explain why?
Here is my code for both methods
def simpleLine(x1, y1, x2, y2):
dy = y2-y1;
dx = x2-x1;
x = x1
m = dy/dx;
b = y1-m*x1;
if(x1>x2):
x1,x2 = x2,x1
x=x1
while(x<=x2):
y=m*x+b;
PutPixle(win,x,round(y));
x=x+1
'
def BresenhamLine(x1, y1, x2, y2):
dx = abs(x2 - x1)
dy = abs(y2 - y1)
p = 2 * dy - dx
duady = 2 * dy
duadydx = 2 * (dy - dx)
x = x1
y = y1
xend = x2
if(x1 > x2):
x, y,xend = x2, y2,x1
while(x < xend):
PutPixle(win,x,y)
x =x+1
if(p<0):
p = p + 2*dy
else:
y = y-1 if y1>y2 else y+1
p = p+2*(dy-dx)
Bresenham's algorithm was invented for languages and machines with different performance characteristics than your python environment. In particular, low-level languages on systems where floating point math is much more expensive than integer math and branches.
In Python, your simple version is faster even though it uses floating point and rounding, because Python is slow and it executes fewer python operations per pixel. Any difference in speed between single integer or floating point operations is dwarfed by the cost of just doing python stuff.

How can I find all points between point1 and point 2 - Objective c? [duplicate]

I've got two points between which im drawing a line (x1,y1 and x2,y2) but i need to know the coordinates of x3,y3 which is gapSize away from point x2,y2. Any ideas on how to solve this problem (the program is written in objective-c if that is helpful at all)?
You can simply calculate the angle in radians as
double rads = atan2(y2 - y1, x2 - x1);
Then you get the coordinates as follows:
double x3 = x2 + gapSize * cos(rads);
double y3 = y2 + gapSize * sin(rads);
Is this what you meant?
Compute the distance between P1 and P2: d=sqrt( (y2-y1)^2 + (x2-x1)^2)
Then x2 = (d*x1 + gapSize*x3) / (d+gapSize)
So x3 = (x2 * (d+gapSize) - d*x1) / gapSize
Similarly, y3 = (y2 * (d+gapSize) - d*y1) / gapSize
Sorry for the math. I didn't try to code it but it sounds right. I hope this helps.
There are many ways to do this. Simplest (to me) is the following. I'll write it in terms of mathematics since I can't even spell C.
Thus, we wish to find the point C = {x3,y3}, given points A = {x1,y1} and B = {x2,y2}.
The distance between the points is
d = ||B-A|| = sqrt((x2-x1)^2 + (y2-y1)^2)
A unit vector that points along the line is given by
V = (B - A)/d = {(x2 - x1)/d, (y2-y1)/d}
A new point that lies a distance of gapSize away from B, in the direction of that unit vector is
C = B + V*gapSize = {x2 + gapSize*(x2 - x1)/d, y2 + gapSize*(y2 - y1)/d}

Quaternion addition like 3ds/gmax does with it's quats

A project I'm working on needs a function which mimics 3ds/gmax's quaternion addition. A test case of (quat 1 2 3 4)+(quat 3 5 7 9) should equal (quat 20 40 54 2). These quats are in xyzw.
So, I figure it's basic algebra, given the clean numbers. It's got to be something like this multiply function, since it doesn't involve sin/cos:
const quaternion &operator *=(const quaternion &q)
{
float x= v.x, y= v.y, z= v.z, sn= s*q.s - v*q.v;
v.x= y*q.v.z - z*q.v.y + s*q.v.x + x*q.s;
v.y= z*q.v.x - x*q.v.z + s*q.v.y + y*q.s;
v.z= x*q.v.y - y*q.v.x + s*q.v.z + z*q.s;
s= sn;
return *this;
}
source
But, I don't understand how sn= s*q.s - v*q.v is supposed to work. s is a float, v is vector. Multiply vectors and add to float?
I'm not even sure which terms of direction/rotation/orientation these values represent, but if the function satisfies the quat values above, it'll work.
Found it. Turns out to be known as multiplication. Addition is multiplication. Up is sideways. Not confusing at all :/
fn qAdd q1 q2 = (
x1=q1.x
y1=q1.y
z1=q1.z
w1=q1.w
x2=q2.x
y2=q2.y
z2=q2.z
w2=q2.w
W = (W1 * W2) - (X1 * X2) - (Y1 * Y2) - (Z1 * Z2)
X = (W1 * X2) + (X1 * W2) + (Y1 * Z2) - (Z1 * Y2)
Y = (W1 * Y2) + (Y1 * W2) + (Z1 * X2) - (X1 * Z2)
Z = (W1 * Z2) + (Z1 * W2) + (X1 * Y2) - (Y1 * X2)
return (quat x y z w)
)
Swapping q1 & q2 yields different results, quite neither like addition nor multiplication.
source

how to find a point in the path of a line

I've got two points between which im drawing a line (x1,y1 and x2,y2) but i need to know the coordinates of x3,y3 which is gapSize away from point x2,y2. Any ideas on how to solve this problem (the program is written in objective-c if that is helpful at all)?
You can simply calculate the angle in radians as
double rads = atan2(y2 - y1, x2 - x1);
Then you get the coordinates as follows:
double x3 = x2 + gapSize * cos(rads);
double y3 = y2 + gapSize * sin(rads);
Is this what you meant?
Compute the distance between P1 and P2: d=sqrt( (y2-y1)^2 + (x2-x1)^2)
Then x2 = (d*x1 + gapSize*x3) / (d+gapSize)
So x3 = (x2 * (d+gapSize) - d*x1) / gapSize
Similarly, y3 = (y2 * (d+gapSize) - d*y1) / gapSize
Sorry for the math. I didn't try to code it but it sounds right. I hope this helps.
There are many ways to do this. Simplest (to me) is the following. I'll write it in terms of mathematics since I can't even spell C.
Thus, we wish to find the point C = {x3,y3}, given points A = {x1,y1} and B = {x2,y2}.
The distance between the points is
d = ||B-A|| = sqrt((x2-x1)^2 + (y2-y1)^2)
A unit vector that points along the line is given by
V = (B - A)/d = {(x2 - x1)/d, (y2-y1)/d}
A new point that lies a distance of gapSize away from B, in the direction of that unit vector is
C = B + V*gapSize = {x2 + gapSize*(x2 - x1)/d, y2 + gapSize*(y2 - y1)/d}