Writing a law of cosines calculation [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I'm trying to make a function in C that returns the length of the side of a triangle that is opposite to a given angle, using the Law of cosines.
Now I got the formula to work in excel and it gave the correct results. However when I tried it in C I am getting the wrong results, and I can not work out why.
For the test I have sideA as 21.1, sideB as 19 and the angle between them as 40 degrees. Now the answer should be 14.9 like I get in excel. however in C I'm getting 23.735. Please some one help me work out where I went wrong
// Find the length of a side of a triangle that is oppisit a given angle using the Law Of Cosine
// for example using an triangle that is 21.1cm on one side, 19 cm on the other and an angle of 40 degreese inbetween then....
// in excel it worked and the formuler was =SQRT(POWER(23.1;2)+POWER(19;2)-2*(23.1)*(19)*COS(40*(3.14159/180))) = 14.9 cm
float my_Trig_LawOfCos_OppSideLength(float centerAngle, float sideA, float sideB)
{
float sideLengthPow2= (pow(sideA,2) + pow(sideB,2))) - ((2*sideA*sideB)*cos(centerAngle*(3.14159/180));
float sideLength = sqrt(sideLengthPow2);
return sideLength;
}

This happens if you pass the arguments in the wrong order. You put the side length 23.1 in the position of the angle.
def oppside(ang, lA, lB): return (lA**2+lB**2-2*(lA)*(lB)*cos(ang*(pi/180)))**0.5
oppside(40,19,23.1)
>>> 14.905575729577208
oppside(19,23.1,40)
>>> 19.65430416708927
oppside(23.1,19,40)
>>> 23.72490935854042
Quite often you can find such errors by producing a minimal executable example that shows the wrong result, because then you would also document the erroneous function call (and perhaps even see it for yourself).

Related

calculate sqrt using nested intervals in c [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 months ago.
Improve this question
I have to write a script in C where the sqrt of an positive number is calculated using nested intervals and call by reference (no structs or math.h allowed)
I tried for like 5 days but I'm kina depressed now. Can anyone help?
I can show you how to calculate the square root of 5. From there you'll know what to do:
First, you know that the square root of a number is generally smaller than the number itself, so you start with the interval [0,5], and you calculate the middle (which is 2.5, whose square is 6.25. So, the square root of 5 should be between 0 and 2.5. So you get:
interval : [0, 2.5]. Middle=1.25. Square(middle)=1.5625. Square(middle)<5 => this becomes the lower bound:
interval : [1.25, 2.5]. Middle=1.875. Square(middle)=±3.5. Square(middle)<5 => this becomes the lower bound:
interval : [1.875, 2.5]. ...

Model 3D spring from points [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
I have generated points like this and now I what to connect all these points into one model - spring. How can I achieve this? I've tried iterating through each point and build it from polygons or triangles but I have failed.
I have set of rings where each ring was build from points which coords I have.
You probably want to treat these as generalized cylinders and tessellate a triangle mesh. This can be done by sweeping a circle along the path. Some of the details are tricky since undefined tangents can lead to unexpected twists in your triangle mesh. You might want to study the GLE library or the TubeGeometry implementation in ThreeJS.
For simplestic rendering, note that OpenGL has GL_LINE_STRIP. It also has glLineWidth, although many platforms have a max width of 1. You would need to take care to use separate draw calls for seperate springs, otherwise they'll be connected.

How to implement bit error based on percentage in C? [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 7 years ago.
Improve this question
I'm stuck at trying to simulate an Binary Symmetric Channel in C.
It should work like this: the user enters a number (for example 0.01 = 1%) which represents error rate. So, for instance, if i read 1001 from file every bit has a chance to change its value to 0/1 respectively depending on the entered percent.
Reading from file and writing into another is already working, but I just don't know how to make these percentage-based errors happen.
Any help is much appreciated, thanks in advance.
For generating the percentage-based error, you could do something like this:
double rate = get_rate(); // userinput between 0.0 and 1.0 for 0% - 100%
do {
double nr = drand48(); // Generates a number between 0.0 and 1.0
if (nr < rate) {
// Generate biterror here
}
} while (some_more_bits_to_check);

x-y plot using C language [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 3 years ago.
Improve this question
I know there are already other posts about this, but I am unable to get the recommended libraries running.
I am writing a program in C. I am fairly inexperienced at programming. I need to plot the results on an x-y graph. There will be potentially hundreds or even thousands of points. The points will be plotted as the program calculates them, so the graph may need to scroll sideways if the x-axis is exceeded.
So, basically, what I need to do is open a window with x-y axes and plot points in this graph as my program comes up with the numbers. I am looking for the simplest and quickest way to get this written, and it's just a way for me to visualise the results. Can C handle this or do I need a library? If a library, I need one that is easy to set up as my experience is limited.
Thanks in advance
Andrew
If you look for a library to be linked into yours program then MathGL (cross-platform GPL plotting library) is better than gnuplot. At this, it can handle huge data sets, can collect plotting (i.e. add plot, add new plot, add new plot, ..., save current result/plot, add new plot, ..., save result). And MathGL have C interface too.
I'm a bit confused by words "so the graph may need to scroll sideways if the x-axis is exceeded". Because it is difficult to place a point (plot) if one don't know the final axis scale(s).
May be you need just a bitmap (or XPM image - 2D array of char) each row/column is proportional to time-step and the height of point is proportional to data value, like
h[i] = Height*(y[i]-ymin)/(ymax-ymin).
I would use gnuplot if on *nix
http://ndevilla.free.fr/gnuplot/
http://ndevilla.free.fr/gnuplot/gnuplot_i/index.html
Looks pretty easy to me

Camera calibration across multiple images [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
Taking one camera and moving it around to take two images of the same object, from a different viewpoint, one should be able to compute a matrix that relates these two scenes. In OpenCV, how is this accomplished?
If said object is a calibration pattern like the chessboard used by OpenCV, then the camera calibration routine mentioned by ChrisO would give you both the camera intrinsics (focal length, principal point, and lens distortion) as well as the camera extrinsics (where they are relatively in space).
If you have general object, then you need to establish a set of 2D correspondences which you can feed into cvFindFundamentalMat. This finds the fundamental matrix which relates the two perspectives. Namely, for each point x in camera 1 and corresponding point x' in camera 2, x'Fx = 0. You can similarly find the epipoles, etc. This uses the 8 point algorithm which requires at least 8 point pairs of point correspondences.
You can get the correspondences either manually or with a robust feature extractor and matcher along the lines of MSER/Affine Harris + SIFT.

Resources