Intersection points of two circles in c++ [closed] - c++

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.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Let define a circle in 2D:
struct Vec2D
{
float x;
float y;
};
// Assume dot, normalize, length, sum, subtract, and scale to exist.
struct Circle2D
{
Vec2D center;
float radius;
};
Given two circles, it is needed to determine the 0, 1 or 2 intersection points between the circles:
bool circleVsCircleIntersection( const Circle& a, const Circle& b,
std::array<std::optional<Vec2D>,2>& intersPos);
How to determine the 0, 1 or 2 intersection points of the circles in C++?

Assuming you have a circles center and it's radius
you can setup an equation like
(x-x1)²+(y-y1)²=r1²
you can do this for both circles
I x²-2xx1+x1²+y²-yy1+y1²=r1²
II x²-2xx2+x2²+y²-yy2+y2²=r2²
then you can insert II in I and there you go :)

Related

How can I do this more elegantly? [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 7 years ago.
Improve this question
I have a 2D point A inside the [0,1]² square.
The square is divided into 9 subsquares (of equal dimensions)
http://www.noelshack.com/2015-23-1433689273-capture.png
I want to know which subsquare the point A belongs to.
I can do a if elseif else on the first coordinate, then inside each branch, another if else if else on the second coordinate.
There is a lot of code repeating (the check on the second coordinate)
Is there a better way ?
The trouble is, it is not clear what your 2D point is, what you want the sub-square value as, etc. So a definitive answer is difficult. But anyway, I will make some assumptions and see if I am right about what you are asking...
Assuming you had a point A with a coordinate such as:
float point[2] = {0.1224, 0.4553}
Then to work out where it is inside the square you can do some simple maths:
float x = point[0] * 3;
float y = point[1] * 3; //Multiply both by 3
int xIdx = floor(x);
int yIdx = floor(y); //Floor the result - this gives a number 0 to 2
int cell = yIdx * 3 + xIdx + 1; // Calculate the cell index (based on your diagram)
Now you could generalise this for any point - for example the point might be (1.23343, 2.6768) - by simply removing the integer part from the point - leaving a number between 0 and 1. The integer part would be the super cell, and the fractional part would be converted into the sub cell as above.

How to compute a definite Integral in C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Needs more focus Update the question so it focuses on one problem only by editing this post.
Improve this question
How can you define a function to calculate the value of a definite integral in C++? For example to solve the integral of the function x^2 * cos(x)?
Interestingly enough, I ran across this article a little while ago explaining one method for calculating numerical integrals using function pointers.
https://helloacm.com/c-function-to-compute-numerical-integral-using-function-pointers/
For something like x^2 * cos(x):
You would need an overloaded integral function:
double integral(double(*f)(double x), double(*g)(double x, double y), double a, double b, int n)
{
double step = (b - a)/n; // width of rectangle
double area = 0.0;
double y = 0; // height of rectangle
for(int i = 0; i < n; ++i)
{
y = f(a + (i + 0.5) * step) * g(a + (i + 0.5) * step, y);
area += y * step // find the area of the rectangle and add it to the previous area. Effectively summing up the area under the curve.
}
return area;
}
To call:
int main()
{
int x = 3;
int low_end = 0;
int high_end = 2 * M_PI;
int steps = 100;
cout << integral(std::powf, std::cosf, low_end, high_end, steps);
return 0;
}

Image Borders & Corners C++ [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 7 years ago.
Improve this question
I'm working on openCV C++ project ,
Part of my project requires to point at any pixel of image with my mouse, get it's x, and y coordinates then I should copy a 8*8 Block of pixels around this pixel to apply some image processing functions for this block.
This is a part of my code that take 8*8 block around pixel:
cv::Mat foo = Mat(8, 8, CV_8UC3);
foo = img3.colRange(x-4, x + 4).rowRange(y-4, y + 4);
But now I have a problem with image borders; if the mouse on a pixel near one of image borders or corners I have an exception because the range of col & rows (The Block size becomes bigger than existing image).
How can I solve this problem?
Just clamp the x and y values so that there are always 4 pixels around them:
x = max(4, min(img3.cols - 5, x))
y = max(4, min(img3.rows - 5, x))
cv::Mat foo = img3.colRange(x-4, x + 4).rowRange(y-4, y + 4);

Algorithm to detect if 2 cuboid are colliding giving their bounds [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 4 positions, with X, Y and Z component. The first 2 vectors represent the 2 extents, and the other vector represent the other 2 extents of the other cuboid.
An example:
Vector3 firstCubeMax = Vector3(10, 10, 10);
Vector3 firstCubeMin = Vector3(-10, -10, -10);
Vector3 secondCubeMax = Vector3(0, 0, 0);
Vector3 secondCubeMin = Vector3(-30, -60, -30);
(first cuboid starts at 0,0,0 with size (20,20,20). The second one starts at (15, 30, 15) and has a size of (30,60,30))
What I want to do is to check if 2 cuboids are colliding (touching or going through) giving those vectors. Also, I am using C++
If the right side of one cuboid is before the left side of other, or the top of one cuboid is below the bottom of the other, or the front of one cuboid is behind the back of the other, then they do not intersect. Otherwise, they do.
So negate the "ors" into a bunch of "ands" to get:
return firstCubeMin.x() < secondCubeMax.x()
&& secondCubeMin.x() < firstCubeMax.x()
&& firstCubeMin.y() < secondCubeMax.y()
&& secondCubeMin.y() < firstCubeMax.y()
&& firstCubeMin.z() < secondCubeMax.z()
&& secondCubeMin.z() < firstCubeMax.z()
Put another way, they intersect if and only if they intersect when projected onto all three axes. The "only if" direction is obvious; you can prove the "if" direction by thinking about what it means for a point to be inside a cuboid.

Algorithm to generate a point grid out of a plane equation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a plane equation in 3D-space: ax + by + cz + d = 0 and I want to fill this plane within a given radius from a specific point on the plane with regulary distributed points. It seems to me, that there should be a mathematical elegant answer, but I fail to see it. Answer in C++ or pseudo-code will be better.
I'll assume you have a reasonably good 3d vector class, and call it vec3 in the answer. The first thing you need is a vector in your plane. There are a few ways to generate one given the normal-plane equation, but I prefer this one:
vec3 getPerpendicular(vec3 n)
{
// find smallest component
int min=0;
for (int i=1; i<3; ++i)
if (abs(n[min])>abs(n[i]))
min=i;
// get the other two indices
int a=(min+1)%3;
int b=(min+2)%3;
vec3 result;
result[min]=0.f;
result[a]=n[b];
result[b]=-n[a];
return result;
}
This construction guarantees that dot(n, getPerpendicular(n)) is zero, which is the orthogonality condition, while also keeping the magnitude of the vector as high as possible. Note that setting the component with the smallest magnitude to 0 also guarantees that you don't get a 0,0,0 vector as a result, unless that is already your input. And in the case, your plane would be degenerate.
Now to get your base vectors in the plane:
vec3 n(a,b,c); // a,b,c from your equation
vec3 u=normalize(getPerpendicular(n));
vec3 v=cross(u, n);
Now you can generate your points by scaling u and v and adding it to the vector you got on the plane.
float delta = radius/N; // N is how many points you want max in one direction
float epsilon=delta*0.5f;
for (float y=-radius; y<radius+epsilon; radius+=delta)
for (float x=-radius; x<radius+epsilon; radius+=delta)
if (x*x+y*y < radius*radius) // only in the circle
addPoint(P+x*u+y*v); // P is the point on the plane
The epsilon makes sure your point count is symmetric and you don't miss the last point on the extremes.