How can I do this more elegantly? [closed] - c++

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.

Related

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);

Defining hypotenuse of a triangle in C++ [duplicate]

This question already has answers here:
How to determine if a point is in a 2D triangle? [closed]
(25 answers)
Closed 9 years ago.
I am working on an assignment in which we are given a (my_x,my_y) coordinate and we have to tell whether this coordinate lies in the free space or inside an obstacle.
As you can see, from the picture, I have to tell whether a certain point lies among any of the obstacle.
Checking for out of boundary is easy and simple.
Similarly I check for circle as (pseudo code):
if sqrt((my_x-5)^2+(my_y-3.5)^2) <= 0.5
this means it is inside circle.
if ((my_x >= 3.5 || my_x <= 6.5) && ((my_y >= 5 || my_y <= 6)
this means it is inside rectangle.
However I am stuck for the triangle case. The main reason is that my_x and my_y are of decimal type and can take any value suppose up to 2 decimal figures. Now one was is to have several if conditions and then check each.
I want to know is there is some better algorithm to define the triangle may be using equations and what it might be.
You can you the concept of vector products to find if a point is inside a triangle or not :-
suppose point is (x,y) which you need to check. (x1,y1),(x2,y2),(x3,y3) are three vertices of the triangle. then each triple ((x1,y1),(x2,y2),(x,y)),((x2,y2),(x3,y3),(x,y)),((x3,y3),(x1,y1),(x,y)) are of same sign.
vector product of (x1,y1),(x2,y2),(x,y)
vp = (x-x2)*(y1-y2) - (y-y2)*(x1-x2)
Hence using same equation for all triples :-
sign1 = sign((x-x2)*(y1-y2) - (y-y2)*(x1-x2))
sign2 = sign((x-x3)*(y2-y3) - (y-y3)*(x2-x3))
sign3 = sign((x-x1)*(y3-y1) - (y-y1)*(x3-x1))
if(sign1==sign2==sign3) { //inside
return(true);
}
else return(false) // outside

Fastest way to interpolate between radians? [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
So, I have radian angles without any range (-inf to +inf basically) and I need to interpolate these as quick as possible. If there any cookie cutter way to do this?
PS: I only need to interpolate 2 values at a time, so a+f*(b-a) basically
PPS: the output does NOT have to be in any specific range (-PI to PI or 0 to 2PI)
PPPS: The specific problem is how to do the wrapping of the values around -PI/+PI and their multiples most efficiently
BETTER Actually forget what I wrote first. You can simply do:
template<typename K>
K lerpAngle(K u, K v, K p) {
return u + p*wrapMP(v - u);
}
where wrapMP moves an angle to the interval [-pi|+pi]. Both input u and v can be any radian angle and the result will also not be in a specific interval.
The idea is actually quite simple. Move your point of view to the point u such that u=0. Now v can be arbitrary as angles are not normalized, but we just wrap the distance v-u = v to [-pi|+pi] and walk by the given percentage p into that direction.
OLD (and inefficient) I wrote this code once:
template<typename K>
K lerpAngle(K u, K v, K p) {
K a = wrap2Pi(u);
K b = wrap2Pi(v);
K d = b - a;
if(d < -PI) {
b += 2*PI;
}
if(d > +PI) {
b -= 2*PI;
}
return wrap2Pi(a + p*(b - a));
}
where wrap2i moves an angle to the interval [0,2*pi[.
First, "normalize" your input angles so they're in a specific range, like (-π, π]. If you do this already, skip this part.
Then, take their difference. If the absolute value of this difference is greater than π then adjust one of the inputs by adding / subtracting 2π such that the absolute value of the difference becomes less or equal to π.
Then simply interpolate between those two values.
If the output of this interpolation should always be in the range (-π, π] again (note: it probably doesn't have to, depending on where you need this angle), then add / subtract 2π again if it's not.

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.