OpenGL: Draw line with point and directon vector [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 4 years ago.
Improve this question
I had seen OpenGL statement to draw a line using two points. However, my requirement is to draw a line using the following detail
a point on a line
Direction Vector
Im developing function in c++ using openGL library.
Any help is most appreciated.

The answer depends on the semantics of what you've termed a direction vector.
In the computer graphics context I would normally take that term to mean a unit vector facing in the specified direction. Whereas in a mathematics context you might simply mean the relative vector that results from subtracting the two points' coordinates.
[Using P1 and P2 to represent the required two points, and V for the vector].
In the former case, you also need a specify a length for the vector, so you'll need:
P2 = P1 + n * V
whereas in the latter case, it's just trivially
P2 = P1 + V

Just make that two-point line a very long one, say 10000 to each direction from your point-on-a-line:
void drawLinePointDirection(Point P, Vector D) {
Point A = P + 10000*D;
Point B = P - 10000*D
drawLineTwoPoints(A, B);
}
assuming D is unit length.

Related

Generating normal distribution weighing constants in a range [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have some results whose relevance decreases with distance. I want to weight the result array elements with constants whose distribution is close to normal or folded normal. At start I want to generate an array with N constants starting from 1 to 0.01 by a function.
The result should be something like the following, ending with a number close to 0.01.
const double normalDistWeight[] = {
1.000, 0.997, 0.994, 0.989, 0.984, 0.977, 0.970, 0.961, 0.951, 0.939,
0.926, 0.910, 0.893, 0.874, 0.853, 0.830, 0.805, 0.778, 0.750, 0.719,
0.687, 0.654, 0.619, 0.584, 0.548, 0.512, 0.476, 0.440, 0.405, 0.370,
0.337, 0.305, 0.274, 0.246, 0.219, 0.194, 0.171, 0.150, 0.131, 0.114,
0.098, 0.085, 0.073, 0.063, 0.054, 0.047, 0.040, 0.035, 0.030, 0.027
};
Unfortunately I can't use any third party libraries or C++11 features, only plain C++.
Edit: Oh, I was over-thinking it... It's just a simple Gaussian error, so exp(-x^2) should work.
It appears to me that all you want is an array of values of the Gaussian function corresponding to uniformly spaced points on the positive half-axis, up to a point where the value is about 0.01.
This is straight-forward. The Gaussian function is f(x) = exp(−x2), like this:
In the chosen expression, we already have f(0) = 1, so all that remains is to find the final point x where we have f(x) = 0.01. Invert: x = √−log(0.01) ≈ 2.15.
So all you need to do is evaluate f on uniformly spaced points on the interval [0, 2.15].

C++ program for power but without using pow function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm a newbee in C++ and I'm writing a C++ program that asks the user to input two integers and then it raises the first integer to the power specified by the second integer. For example, if the user enters 5 and 8, the result will be 5 subscript 8, i.e., number five will be raised to the eighth power. The program must not use any pre-defined C++ functions (like pow function) for this task. The program should allow the user to perform another calculation if they so desire. Can anyone help
I'm not going to give you any code, because that won't allow you to truly explore this concept. Rather, you should use this pseudo code to implement something on your own.
Create a function which accepts two inputs, the base and the exponent.
Now there are several ways to go about doing this. You can use efficient bit shifting, but let's start simple, shall we?
answer = base
i = 1
while i is less than or equal to exponent
answer = answer * base
return answer
Simply loop through multiplying the base by itself.
There are other ways that focus on efficiency. Look here to see something that you may want to attempt: are 2^n exponent calculations really less efficient than bit-shifts?
The program must not use any pre-defined C++ functions (like pow function) for this task
You can use some piece of c++ code like follows, to compute xy, without using any predefined function:
int x = 5;
int y = 3;
int result = 1;
for(int i = 0; i < y; ++i)
{
result *= x;
}
cout << result << endl;
Output:
125
See a working sample here.

Value interpolation in 2d from scattered data [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I'm looking for a way to interpolate values from some 2D scattered data. I have a 3d points that represent a terrain from which I want to interpolate intermediate points. For input (X,Y) coordinates I need Z (height) value.
This article on wikipedia may also help you understand my wishes. There is a library in matlab called triscateredinterp that I think it does what I want.
What is a lightweight way to accomplish this interpolation in C++?
I don't think you need 3D interpolation (triscateredinterp). You have data based on 2D inputs; the 3rd dimension is your output. If I understand correctly you want to provide a point in 2D (something between the original points, and interpolate the value.
Light weight? nearest neighbor!; then bi-linear interpolation; then bi-cubic (and others). The first is simple, the others require an increasing amount of math.
Bi-linear: For each point to be interpolated, find the nearest 3 points to your X and Y:
lat long Altitude
X1 Y1 A1
X2 Y2 A2
X3 Y3 A3
Make these matrices:
X1 Y1 1 A1
X = X2 Y2 1 Y = A2
X3 Y3 1 A3
B is the interpolation coefficients we will calculate for those three nearest points (and can be re-used for all points in the area)
B1
B = B2
B3
The matrix equation is: X*B = Y
You could use brut force:
Multiply both sides by XT: XT*X*B = XT*Y
Take the inverse of XT*X: B = (XT*X)^-1 *XT*Y.
Yes 3x3 matrix inversion. Tying this back to a C++ question, you might use Boost for your matrix operations.
Here is another similar C++ question: Solving a system of equations programmably?
One problem that can arise from the bi-linear technique is that as your interpolated point becomes closer to a different set of 3 values you can get some jumps (how would you interpolate 4 points in a saddle configuration?)
One good method for scattered points is Natural Neighbor Interpolation.
You can check the implementation available in CGAL for example : http://doc.cgal.org/latest/Interpolation/index.html

Which of the following implementations is faster? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What I'm trying to do is fill the values of a specific matrix using unknown variables.
Here's the first implementation:
#define PHI(I,J,K) phi[xlength*ylength*(I)+xlength*(J)+(K)] //Macro that calls function
Where phi is a 1D matrix of dimensions xlength*ylength*tlength
or
phi= new double[xlength*ylength*tlength]; //code for phi
The other option is to define a function like
void PHI(double *&phi, int &I, int &J, int &K, double &value) //declare function
{
phi[xlength*ylength*I+xlength*J+K]=value; //return specific value of phi
}
I would use the macro or function in something like this:
for (int i=0;i<tlength;i++) //just making a loop here
{
for (int j=0;j<ylength;j++)
{
PHI(i,j,1)= stuff here //The macro or the function would go here
}
}
So what I'm doing is either using a macro to point to a specific cell of the matrix phi[] or I'm using a function to fill a specific value of the matrix phi[].
Which is faster?
Most likely no difference at all. The compiler will inline the function just as much as it inlines the macro. Since macros are much harder to use in a debugger, use a function.
And as I always say in case of "which performs better", you should always benchmark the different options, since differences in compilers can make some small difference in some cases (and in other cases make a big difference). Asking on SO or some other internet site will only tell you what other people think, not what actually happens in your real case.

Recursive dynamic programming approach for the travelling salesman [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 9 years ago.
Improve this question
I was hoping someone could explain how the code on this page works: TSP-Recursive
The pseudocode is hard to interpret, and the dynamic programming approach makes it particularly difficult to understand. Why is the bit shift needed? How can this approach be generalized (for example, given coordinates for locations, can we adapt this approach to solve that problem)?
The bit shifting is because the code is using an int to represent a set, specially the set of visited cities. If you have 32-bit integers then an int can represent a set of up to 32 items.
The basic operations are
// add n to set
set |= 1 << n;
// remove n from set
set &= ~(1 << n);
// test set for n
if ((set&(1 << n)) != 0)
...
The variable graph is a map (in the mathematical sense). Given two cities, A and B, graph[A][B] is the distance from A to B.
The variable dp is another map. Given a set of cities S, and a city A, dp[S][A] is the shortest journey visiting each city in S and ending at A.
Once graph has been filled in and the final city has been chosen, the function init fills in some of the distances in dp: for each city A, the shortest journey starting at A and visiting only B is obviously just graph[A][B].
The function TSP( S, X ) gives the length of the shortest journey visiting every city in S and ending at X. If that distance is already listed in dp, return that. Otherwise, for each city A in S, calculate the length of the shortest journey that visits every other city in S, then A, then X. The shortest of those is the answer, so the function records it in dp and also returns it.