Sides of triangle based on coordinates in C - c

So this code should calculate the size of a and b side and Height of triangle and I am too dumb to realize what is wrong, can someone help me locate misstake? Thanks for help.
#include <stdio.h>
#include <math.h>
float distance(float, float, float, float);
int main()
{
float a, b, c, d;
float aX, aY, alfa;
float bX, bY, beta;
scanf("%f %f %f" , &aX, &aY, &alfa);
scanf("%f %f %f", &bX, &bY, &beta);
distance(aX, aY, bX, bY);
float gamma = 180 - alfa - beta;
c = distance(aX, aY, bX, bY);
b = ((c/sin(gamma)) * sin(beta));
a = ((c/sin(gamma)) * sin(alfa));
d = (sin(alfa))*c;
printf("Side a:%.2f\n Side b:%.2f\n Side c:%.2f Height:%.2f\n", a, b, c, d);
return 0;
}
float distance(float x1, float y1, float x2, float y2)
{
float dx = x2 - x1;
float dy = y2 - y1;
return sqrt(dx*dx + dy*dy);
}

The problem is that you use degrees instead of radians. Trigonometric functions have to be applied to values measured in radians.

Thank you. I am just a student so I do not know how these functions work at all. But I could read it on the website where I find it ... I am soo dumb ...

Related

How to use the proper cross product?

So a cross product is v1 = <x1, y1, z1> and v2 = <x2, y2, z2> defined as v1v2 = <y1z2 - z1y2, z1x2 - x1z2, x1y2 - y1*x2> any a output of 32.000000. But I don't know if my code is wrong or wrong it the wrong way. But I'm not getting an weird text. Can anyone help me find the problem in the code?
void cross_product( float x1, float y1, float z1,
float x2, float y2, float z2,
float* result_x, float* result_y, float* result_z){
float d = ((y1*z2)-(z1*y2), (z1*x2)-(x1*z2), (x1*y2) - (y1*x2));
printf("v1 (dot) v2 = %f", d);
enter image description here
As said by kaylum, you need to store the result for each vector in one of your variables return_ called by reference.
If you want to calculate de distance between your two points in float d, you have to make the square root of the sum of the powers of two of the distance between each coordinate.
At the end, your code should look like this:
void cross_product( float x1, float y1, float z1,
float x2, float y2, float z2,
float* result_x, float* result_y, float* result_z)
{
*result_x = (y1 * z2) - (z1 * y2);
*result_y = (z1 * x2) - (x1 * z2);
*result_z = (x1 * y2) - (y1 * x2);
float d = sqrtf(powf(x2 - x1, 2) + powf(y2 - y1, 2) + powf(z2 - z1, 2));
printf("v1 (dot) v2 = %f", d);
}
The comma operator evaluates each sub-expression and the final result is the last sub-expression. So in your case it means d only ends up with the result of (x1*y2) - (y1*x2).
For the cross product you need to treat each component result seperately. That is why the API has the output result variables. Store the result into those variables:
*result_x = (y1 * z2) - (z1 * y2);
*result_y = (z1 * x2) - (x1 * z2);
*result_z = (x1 * y2) - (y1 * x2);

Find coordinates of third point in triangle with given coordinates of other two sides and its angles in C

Hello i have homework triangulation.c with two watchovers and boat.
I have to find distance of this boat, and her coordinates.
I have coordinates of x1,y1,x2,y2,alpha,beta i know how to calculate distance between watchtowers, distance to boat but not her coordinates
here is code i have
float to_radians(const int angle);
float get_watchtowers_distance(const int x1, const int y1, const int x2, const int y2);
float get_boat_distance(const float d, const int alpha, const int beta);
int main() {
int x1 = 76;
int y1 = 316;
int x2 = 57;
int y2 = 516;
int alpha = 17;
int beta = 17;
float d = get_watchtowers_distance(x1,y1,x2,y2); //200.90
float b = get_boat_distance(d, alpha, beta); //30.71
float x3 = ? //97.07
float y3 = ? //418.90
printf("%.2f %.2f %.2f", b, x3, y3);
return 1;
}
can you help me with some formula to get x3 and y3 coordinates? thank you

C function returns 0 when shouldn't [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 4 years ago.
Improve this question
I'm making a set of functions in C for simulate a microcanonial ensamble (a periodic-bounded box with N particles with fixed Energy and Volume). The point is that one of my main functions is to get the distance between 2 particles, and sometimes it returns 0 even if the particles are not that close.
The function is
// Determinar la mínima distancia dadas dos coordenadasy una longitud de caja periodica
float pdist(float x1, float y1, float x2, float y2, float a) {
float Dx = abs(x2-x1);
float Dy = abs(y2-y1);
if(Dx >a/2) Dx = a-Dx;
if(Dy >a/2) Dy = a-Dy;
return sqrt(pow(Dx,2)+pow(Dy,2));
}
If you want to see all the code, you can see it in
https://gitlab.com/jarr.tecn/statistical_mechanics
in the file box.h
and one example in ejemplo.h
The problem comes from abs function which will return an integer:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float pdist(float x1, float y1, float x2, float y2, float a) {
float Dx = abs(x2-x1);
float Dy = abs(y2-y1);
if(Dx >a/2) Dx = a-Dx;
if(Dy >a/2) Dy = a-Dy;
return sqrt(pow(Dx,2)+pow(Dy,2));
}
float pdist2(float x1, float y1, float x2, float y2, float a) {
float Dx = fabsf(x2-x1);
float Dy = fabsf(y2-y1);
if(Dx >a/2) Dx = a-Dx;
if(Dy >a/2) Dy = a-Dy;
return sqrt(pow(Dx,2)+pow(Dy,2));
}
int main(void)
{
printf("first form: %f\n", pdist(.1, .1, .2, .2, .5));
printf("second form: %f\n", pdist2(.1, .1, .2, .2, .5));
}
Gives:
first form: 0.000000
second form: 0.141421

Trying to find distance between two points, but I'm getting wrong answers:(

I am trying to find distance between two points, but the result is off.
If i type in (1,2) and (4,5) I get
Distance = 1.414214
instead of 4.242640
This happens regardless of what numbers I input.
#include<stdio.h>
#include<math.h>
float distance(float a, float b, float c, float d);
int main()
{
float a,b,c,d,D;
printf("Please enter the first x coordinate. x1= ");
scanf("%f",&a);
printf("Please enter the first x coordinate. y1= ");
scanf("%f",&b);
printf("Please enter the first x coordinate. x2= ");
scanf("%f",&c);
printf("Please enter the first x coordinate. y2= ");
scanf("%f",&d);
D = distance(a,b,c,d);
printf("Distance = %f",D);
return 0;
}
float distance(float x1, float x2, float y1, float y2)
{
float d, D, x, y, X, Y;
x = x1 - x2;
y = y1 - y2;
X = x*x;
Y = y*y;
d = X + Y;
float sqrtf (float d);
return sqrtf(d);
}
You're getting the wrong answer because you're passing the wrong arguments to your function.
D = distance(a,b,c,d);
Based on your input scanning, you are telling it this:
D = distance(x1,y1,x2,y2);
However, your function takes a different order of arguments (x1, x2, y1, y2).
Change that line to this:
D = distance(a,c,b,d);
And you should get the right answer.
Look at the variable declaration inside the main function-
int a,b,c,d,D;
The data type be a float instead of integer.

I keep getting the error message: "Conflicting types for 'sqrt' "

I just started an intro to computer programming class, I only know 2 weeks worth of programming.
I keep getting the "conflicting types for 'sqrt' " so I made a prototype and I'm still getting the message. I've tried everything.
#include<stdio.h>
#include<math.h>
float distance(float a, float b, float c, float d);
int main()
{
int a,b,c,d,D;
printf("Please enter the first x coordinate. x1= ");
scanf("%f",&a);
printf("Please enter the first x coordinate. y1= ");
scanf("%f",&b);
printf("Please enter the first x coordinate. x2= ");
scanf("%f",&c);
printf("Please enter the first x coordinate. y2= ");
scanf("%f",&d);
D = distance(a,b,c,d);
printf("Distance = %.4f",D);
return 0;
}
float distance(float x1, float x2, float y1, float y2)
{
float d, D, x, y, X, Y;
x = x1 - x2;
y = y1 - y2;
X = x*x;
Y = y*y;
d = X + Y;
float sqrt (float d);
}
This is a function declaration
float sqrt (float d);
if you want to return the result of the function call you need
return sqrt(d);
also, the conflicting type error is due to the fact that the sqrt function prototype is
double sqrt(double x);
there is a float equivalent
float sqrtf(float x);
so perhaps your function should return
return sqrtf(d);
Note: I don't see any benefit in splitting this calculation so much you could just
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

Resources