Algorithmical issue with point inside circle [closed] - algorithm

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I have a question [ silly - I admit ] about finding wherether the point is in the circle or not, I have a coordinates of the center of circle, and I know the equation, but I'm having the problem with radius, let say it is 2 km, so I have x:46.123654 y: 15.789456 and r=2 or 200 or 2000? What should be the value of R?

For clarity, the units of r should almost certainly be the same as the units of x and y. If, for instance x = 46.123654 means 46.123654 meters, and the radius of your circle is two kilometers, then the value of r should be 2000.0, meaning two thousand meters. You should also be explicit in some comment about what the units are, i.e. x = 46.123654 //meters. If the units are the same, you can apply formulas without confusing conversions, for instance:
//determines whether a point (x, y) is in the circle of radius r centered at (0, 0)
bool isInCircle(double x, double y, double r)
{
return x * x + y * y <= r * r; //pythagorian theorem!
}
This isn't really programming, just middle-school math and common sense.

Related

Holding a max value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to code a function with two inputs (x,Tau). Let's suppose that the vector is a simple x=sin(t). The function must detect the local maximum. If a local max is spotted, it should deteriorate from there with the exp(-t/Tau) until the sin(x) is greater again. From there, the x should be followed until the next local max. It is a kind of a low pass filter, but I cannot code it properly in Octave.
Here is a sample picture drawn by hand:
This question is more mathematics than computing. The following solution will work when your first curve is a smooth function, but I'm not sure how well it would work if your input is a set of experimentally measured values with a high degree of scatter. In that case you might need something more sophisticated.
I assume you want to make the decay curve deviate from the underlying curve not at the maximum but at the point slightly past the maximum where the downward slope of the underlying curve first exceeds that of exponential decay with time constant tau. This seems like a much more physical situation. Switching to exponential decay at the literal maximum could/would result in the decay curve actually crossing the underlying curve.
If your 'sine' function is x(t), and if the decay constant is tau, then you need a new symbol for the red curve in your graph. Call this y(t). Representing the time axis by a vector of numbers t = [t(1), t(2), ..., t(n)] and the corresponding values of your 'sine' function by a vector x = [x(1), x(2), ..., x(n)] and setting y(1) = x(1), then the (i > 1)th member of the vector y is given by max(x(i), y(i-1) exp(-(t(i) - t(i-1))/tau)).
Implementing this in Octave is straightforward:
clear
t = linspace(0, 6*pi, 1000); %Or whatever
x = sin(t); %Could be any function
function y = decay(x, t, tau)
y = zeros(1, length(t));
y(1) = x(1);
for i = 2:length(t)
y(i) = max(x(i), y(i-1)*exp(-(t(i)-t(i-1))/tau));
endfor
endfunction
tau = 10;
y = decay(x, t, tau);
clf
hold on
plot(t, x)
plot(t, y)
hold off
If you want to use the above function regularly it would probably be a good idea to add checks to make sure that the correct number of arguments are passed, that x and t are the same length and that tau is a scalar.

how to calculate shortest distance between two moving objects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
the question is simple one object is moving from east-west with a velocity of v1 and another from south-north with velocity v2.
I just need the algorithm(formula) to calculate the shortest distance between them so I can write a program for it.
I do have distance between them and the meting point of their paths they are d1 and d2.
Assuming you are asking for 2-d space, at t=0, let the starting points be (d1,0) and (0,d2) on the coordinate axes. We can assume this because one object is always moving horizontally (E-W direction, along X-axis) and other vertically (S-N direction, along Y-axis). Now, after some time t, their positions will be,(d1-t*v1) and (0,d2-t*v2). (Speed-Distance-Time relation).
Now, distance between them at this time t will be,
D = d^2 = (d1-t*v1)^2 + (d2-t*v2)^2
So, differentiating both sides wrt t,
dD/dt = 2(-v1)(d1-t*v1) + 2(-v2)(d2-t*v2) ....(1)
For D to be minimum, dD/dt = 0 and second differential must be positive. Now, second differential :
d2D/dt2 = 2*v1^2 + 2*v2^2 which is positive for all real v1/v2. So, if `dD/dt = 0`, distance will be minimum.
So, equating (1) = 0, we get
t = (d1v1 + d2v2)/(v1^2 + v2^2)
So, get sqrt(D) at t = --above value-- and that shall be your answer.
PS: ask these type of questions on mathematics stackexchange.

a function that return the intersection of two rectangles [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Write a function, rec_intersection(rect1, rect2) and returns the
intersection of the two.
Rectangles are represented as a pair of coordinate-pairs: the
bottom-left and top-right coordinates (given in [x, y] notation).
Hint: You can calculate the left-most x coordinate of the
intersection by taking the maximum of the left-most x coordinate of
each rectangle. Likewise, you can calculate the top-most y
coordinate of the intersection by taking the minimum of the top most
y coordinate of each rectangle
This is the answer:
def rec_intersection(rect1, rect2)
x_min = [rect1[0][0], rect2[0][0]].max
x_max = [rect1[1][0], rect2[1][0]].min
y_min = [rect1[0][1], rect2[0][1]].max
y_max = [rect1[1][1], rect2[1][1]].min
return nil if ((x_max < x_min) || (y_max < y_min))
return [[x_min, y_min], [x_max, y_max]]
end
Can someone help explain this code? At least a bit of guidance on the few points where to start deconstructing this code so I can get a start. I've never seen a problem like this one and have been struggling to understand it for the past hour and just can't make any headway.
The intersection of two rectangles is the rectangle where the two overlap. Because of that, you want a rectangle with:
The rightmost of the left-hand sides,
The leftmost of the right-hand sides,
The bottommost of the tops, and
The topmost of the bottoms.
In other words, you want the left, right, top, and bottom that are the closest to the center, but calculating it that way is excessive. So, those are your four x_min (et al) lines.
However, if the rectangles don't overlap at all, there isn't an intersection. That's your return nil line. How do we know there's no overlap? The right and left or the top and bottom aren't where we expect them to be.
The last line just assembles the coordinates (from the first four lines) into the upper-left and lower-right points as specified.

Cocos2d Sprite Movement Speed [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a class which creates a sprite at a random point on the screen, this sprite then moves to the center of the screen.
How can I make it so that it always travels at the same speed?
obviously
CCMoveTo* move = [CCMoveTo actionWithDuration:5 position: ccp(screenWidth/2, screenHeight/2)];
Will always mean the duration is 5 seconds regardless of the distance. But I want the speed to be constant if its travelling 50 pixels or 500 pixels.
Any Help much appreciated
Calculate the duration from the distance to the center.
duration = distance / rate;
Say that moving 50 pixels in 5 seconds is okay. Then your rate is 10 pixels/second.
rate = 10;
If your sprite is at (x,y) then distance is by the pythagorean theorem
dx = x - screenWidth / 2;
dy = y - screenHeight / 2;
distance = sqrt(dx * dx + dy * dy);

Finding points on a line [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
(This question could be better off on math, but im not sure)
http://i.imgur.com/TVINP.png
This is probably very simple but the way I'm thinking of doing it doesn't seem very easy and there must be a simpler method.I've got an image and I want to find some points that fall on a line so in this example image below the starting point of my line is (39,75) and the ending point is (75,142) from there I want to find 5 points (or any number really 5 is just an example) that are all on that line.
Is there some equation I can use that will get me a certain amount of points given any start and end coordinates?
yes.
suppose (x0,y0) and (x1,y1) are the starting and ending points on the line.
t*(x0,y0) + (t-1)*(x1,y1) are also going to be points on that line where t ranges from 0 to 1.
note:
if t = 0, you get (x0,y0)
if t = 1, you get (x1,y1)
if t is any value inside (0,1) you get that "percentage" of the way from (x0,y0) to (x1,y1)
(if t = 0.5, you are halfway between the points)
this is what is often called "tweening" in computer graphics
Yes. Your line segment can be described by this equation:
x = 39 + t * (75 - 39)
y = 75 + t * (142 - 75)
where, t can take on any value between 0 and 1.
So, to get random points on the line, just choose a random value for t (between 0 and 1), and calculate what x, y are.
The idea is, x is traveling from 39 to 75, while y is traveling from 75 to 142, and t represents the fraction of travel that has been completed.
A line can be defined by the function y = mx + b where x and y are coordinates on the cartesian plane, m is the slope of the line defined by (y2 - y1)/(x2 - x1), and b is the point where the line intersects the y-axis.
Given that information and two points on the line, you can fill in the blanks with some basic algebra to determine a function that defines the line. Note that if your coordinate plane for the image places (0, 0) in the top left corner, you may have to flip the sign of the y-coordinate.

Resources