i have an assignment for Programming class.
i have finished 80 percent of it, i am not able to complete the last part.
We have to find the distance between the shore and the boat(Point C) And Coordinates Of The Boat (Point C).
We are given (X,Y) coordinates And Angles of Points A and B.
Given A(76,316) B(57,516) Angle A = 17° and B = 17° Angle.
So Far i found AB= 200.9 AC= 105.04 BC = 105.04 C = 146° And the distance between the shore and the boat d = 30.71.
I tried everything but i am not able to find the Coordinates of Point C (Boat).
The Output is also Given : d = 30.71, Coordinates of C = C(97.07,418.90)
Please help me to find coordinates of Point C.
#include <stdio.h>
#include <math.h>
float radian(int degree);
float get_watchtowers_distance(int x1,int y1,int x2,int y2);
float get_boat_distance(float d,int alpha,int beta);
int main(){
get_watchtowers_distance(76,316,57,516);
get_boat_distance(200.90,17,17);
}
float radian(int degree){
return degree * (M_PI/180);
}
float get_watchtowers_distance(int x1,int y1,int x2,int y2){
return sqrtf(powf(x2-x1,2.0f)+powf(y2-y1,2.0f));
}
float get_boat_distance(float d,int alpha,int beta){
float a,b,c = 180 - (float)alpha - (float)beta;
a = (float)radian(alpha);
b = (float)radian(beta);
c = (float)radian(c);
printf("%.2f %.2f %.2f\n",a,b,c);
float B,A = d*sinf(a)/sinf(c);
B = d*sinf(b)/sinf(c);
float dist,area = 1.0f/2.0f*A*B*sinf(c);
dist = 2*area/d;
printf("AC Distance : %.2f , BC distance : %.2f\n",A,B);
printf("Boat Distance : %.2f\n", dist);
return dist;
}
Simpler approach to find d distance. We can express lengths of the left and right parts of base through d and cotangents
aa = d * ctg(alpha)
bb = d * ctg(beta)
aa + bb = l
d * (ctg(alpha) + ctg(beta)) = l
d = l / (ctg(alpha) + ctg(beta)) =
= l * sin(alpha) * sin(beta) / sin(alpha+beta)
Now you can find C coordinates using normalized vector ab and perpendicular component
abx = (b.x - a.x) / l
aby = (b.y - a.y) / l
aalen = d / tg(alpha)
c.x = a.x + abx * aalen - aby * d
c.y = a.y + aby * aalen + abx * d
There you go I have written some code for your question..
float get_coordinates(int x1, int y1, int x2, int y2, float alpha, float beta){
float a_angle_alpha, b_angle_beta, c_angle_delta;
c_angle_delta = 180 - (float)alpha - (float)beta;
printf("Alpha Angle in Degrees = %.2f\nBeta Angle in Degrees = %.2f\nDelta Angle in Degrees = %.2f\n", alpha, beta, c_angle_delta);
a_angle_alpha = (float)radian(alpha);
b_angle_beta = (float)radian(beta);
c_angle_delta = (float)radian(c_angle_delta);
printf("Alpha in radiance: %.2f\nBeta in radiance: %.2f\nDelta in radiance: %.2f\n", a_angle_alpha, b_angle_beta, c_angle_delta);
int u, v;
float d;
u = x2 - x1;
v = y2 -y1;
printf("u = %.d\nv = %.d\n", u, v);
float d1_distance_AB, d2_distance_BC, d3_distance_AC;
d1_distance_AB = sqrtf((u*u)+ (v*v));
d2_distance_BC = d1_distance_AB*sinf(b_angle_beta)/sinf(c_angle_delta);
d3_distance_AC = d1_distance_AB*sinf(a_angle_alpha)/sinf(c_angle_delta);
printf("Distance between A and B: %.2f\nDistance between B and C: %.2f\nDistance between A and C: %.2f\n",d1_distance_AB, d2_distance_BC, d3_distance_AC);
float RHS1, RHS2;
float x3, y3;
RHS1 = ((x1 * u) + (y1 * v) + (d2_distance_BC * d1_distance_AB * cosf(a_angle_alpha)));
RHS2 = ((y2 * u) - (x2 * v) - (d2_distance_BC * d1_distance_AB * sinf(a_angle_alpha)));
x3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((u * RHS1) - (v * RHS2)));
y3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((v * RHS1) + (u * RHS2)));
printf("Coordinates of Boat at point C is = %.2f:%.2f (x:y)", x3, y3);
float coordinates[2] = {x3,y3};
return coordinates;
}
Full Code:
#include <stdio.h>
#include <math.h>
float radian(int degree);
float get_watchtowers_distance(int x1,int y1,int x2,int y2);
float get_boat_distance(float d,int alpha,int beta);
float get_coordinates(int x1, int y1, int x2, int y2, float alpha, float beta);
int main(){
get_watchtowers_distance(76,316,57,516);
get_boat_distance(200.90,17,17);
get_coordinates(76,316,57,516,17,17);
}
float radian(int degree){
return degree * (M_PI/180);
}
float get_watchtowers_distance(int x1,int y1,int x2,int y2){
return sqrtf(powf(x2-x1,2.0f)+powf(y2-y1,2.0f));
}
float get_boat_distance(float d,int alpha,int beta){
float a,b,c = 180 - (float)alpha - (float)beta;
a = (float)radian(alpha);
b = (float)radian(beta);
c = (float)radian(c);
printf("%.2f %.2f %.2f\n", a, b, c);
float B,A = d*sinf(a)/sinf(c);
B = d*sinf(b)/sinf(c);
float dist,area = 1.0f/2.0f*A*B*sinf(c);
dist = 2*area/d;
printf("AC Distance : %.2f , BC distance : %.2f\n",A,B);
printf("Boat Distance : %.2f\n", dist);
return dist;
}
float get_coordinates(int x1, int y1, int x2, int y2, float alpha, float beta){
float a_angle_alpha, b_angle_beta, c_angle_delta;
c_angle_delta = 180 - (float)alpha - (float)beta;
printf("Alpha Angle in Degrees = %.2f\nBeta Angle in Degrees = %.2f\nDelta Angle in Degrees = %.2f\n", alpha, beta, c_angle_delta);
a_angle_alpha = (float)radian(alpha);
b_angle_beta = (float)radian(beta);
c_angle_delta = (float)radian(c_angle_delta);
printf("Alpha in radiance: %.2f\nBeta in radiance: %.2f\nDelta in radiance: %.2f\n", a_angle_alpha, b_angle_beta, c_angle_delta);
int u, v;
float d;
u = x2 - x1;
v = y2 -y1;
printf("u = %.d\nv = %.d\n", u, v);
float d1_distance_AB, d2_distance_BC, d3_distance_AC;
d1_distance_AB = sqrtf((u*u)+ (v*v));
d2_distance_BC = d1_distance_AB*sinf(b_angle_beta)/sinf(c_angle_delta);
d3_distance_AC = d1_distance_AB*sinf(a_angle_alpha)/sinf(c_angle_delta);
printf("Distance between A and B: %.2f\nDistance between B and C: %.2f\nDistance between A and C: %.2f\n",d1_distance_AB, d2_distance_BC, d3_distance_AC);
float RHS1, RHS2;
float x3, y3;
RHS1 = ((x1 * u) + (y1 * v) + (d2_distance_BC * d1_distance_AB * cosf(a_angle_alpha)));
RHS2 = ((y2 * u) - (x2 * v) - (d2_distance_BC * d1_distance_AB * sinf(a_angle_alpha)));
x3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((u * RHS1) - (v * RHS2)));
y3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((v * RHS1) + (u * RHS2)));
printf("Coordinates of Boat at point C is = %.2f:%.2f (x:y)", x3, y3);
float coordinates[2] = {x3,y3};
return coordinates;
}
Output:
0.30 0.30 2.55
AC Distance : 105.04 , BC distance : 105.04
Boat Distance : 30.71
Alpha Angle in Degrees = 17.00
Beta Angle in Degrees = 17.00
Delta Angle in Degrees = 146.00
u = -19
v = 200
Distance between A and B: 200.90
Distance between B and C: 105.04
Distance between A and C: 105.04
Coordinates of Boat at point C is = 97.07:418.90 (x:y)
To find the distance d directly without determining point C, first, you can calculate the area S of the triangle in two ways.
S = 0.5 * l * d
S = 0.5 * l * l * sin(a) * sin(b) / sin(a + b)
Equating the two and canceling out 0.5 * l gives d = l * sin(a) * sin(b) / sin(a + b).
Related
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);
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
I'm developing a program that calculates the roots of a cubic equation with Tartaglia's method. The problem is when I print the value of the imaginary part, the program rounds the value of 1.54686889 to 1.50.
I have tried to use float instead of double, but, without success.
I'm using Code::Blocks 17.12
My code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <locale.h>
#define PI 3.14159265359
int main(void)
{
setlocale(LC_ALL,"");
double a, b, c, d, p, q, r, t, u, u3, v, v3, A, B, C, D, M, x1, x2, x2i, x3, x3i;
printf("Insira os coeficientes (a b c d): ");
scanf("%lf %lf %lf %lf", &a, &b, &c, &d);
//Verifica se o coeficiente a é válido
if(a != 0){
printf("\nForma geral:\n");
printf("\n (%.2lf)x³ + (%.2lf)x² + (%.2lf)x + (%.2lf) = 0 \n", a, b, c, d);
A = b/a;
B = c/a;
C = d/a;
p = B - (pow(A, 2.0)/3.0);
q = C - ((A*B)/3.0) + (2.0*pow(A, 3.0))/27.0;
D = (pow(q, 2.0)/4.0) + (pow(p, 3.0)/27.0);
if(D < 0){
M = pow(-1.0*D, 1.0/2.0);
r = sqrt(pow(q, 2.0)/4.0 + pow(M, 2.0));
t = acos(-1.0*q/2.0/r);
x1 = 2.0 * pow(r, 1.0/3.0) * cos(t/3.0) - A/3.0;
x2 = 2.0 * pow(r, 1.0/3.0) * cos((t + 2.0 * PI)/3.0) - A/3.0;
x3 = 2.0 * pow(r, 1.0/3.0) * cos((t + 4.0 * PI)/3.0) - A/3.0;
printf("\nAs raízes são: <%lf> <%lf> <%lf>\n", x1, x2, x3);
}
else{
u3 = (-1.0*q/2.0) + pow(D, 1.0/2.0);
if(u3 < 0)
u = -1.0 * pow(-1.0*u3, 1.0/3.0);
else
u = pow(u3, 1.0/3.0);
v3 = (-1.0*q/2.0) - pow(D, 1.0/2.0);
if(v3 < 0)
v = -1.0 * pow(-1.0*v3, 1.0/3.0);
else
v = pow(v3, 1.0/3.0);
x1 = u + v - A/3.0;
D = pow((A + x1), 2.0) + (4.0*C/x1);
x2 = -1.0 * (A + x1)/2.0;
x2i = pow(abs(D), 1.0/2.0)/2.0;
x3 = x2;
x3i = x2i;
if(D < 0){
printf("\nAs raízes são: <%lf>, <%lf + %lfi>, <%lf - %lfi>\n", x1, x2, x2i, x3, x3i);
}
else{
x2 = x2 + x2i;
x3 = x3 - x3i;
printf("\nAs raízes são: <%lf>, <%lf>, <%lf>\n", x1, x2, x3);
}
}
}
else{
printf("O coeficiente <a> não pode ser igual a 0!\n");
}
printf("Fonte de pesquisa: http://www.matematicando.net.br/metodo-de-tartaglia-para-obter-raizes-de-equacao-do-3o-grau/\n");
system("pause");
return 0;
}
The console after printing:
Change the use of abs to fabs and the result is:
As raízes são: <-1.650629>, <-0.174685 + 1.546869i>, <-0.174685 - 1.546869i>
The function abs returns an integer, whereas fabs works on doubles.
y coordinates from user as double variable and calculating them if points forms a triangle but i cant get correct result.
I think there is a problem about using double variables for example if i put integer variable to x1 , y1 it does not calculate mAB
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<conio.h>
int main()
{
double x1,y1,x2,y2,x3,y3;
printf("Enter x , y coordinates of first vertice!\n");
scanf("%lf",&x1);
scanf("%lf",&y1);
/*
if(isInteger(x1)==0)
{
printf("\ndouble");
}
else
{
printf("\nint");
}
*/
/*********************************************/
printf("Enter x , y coordinates of second vertice!\n");
scanf("%lf",&x2);
scanf("%lf",&y2);
/*********************************************/
printf("Enter x , y coordinates of third vertice!\n");
scanf("%lf",&x3);
scanf("%lf",&y3);
/*********************************************/
/*********************************************/
double mAB = (fabs(x1-x2) / fabs(y1-y2));
double mAC = (fabs(x1-x3) / fabs(y1-y3));
printf("\n mAB %lf", mAB);
printf("\n mAC %lf", mAC);
if(mAB == mAC)
{
printf("These points does not forms a triangle!!!!");
}
else
{
/*
* 1-2 AB
* 1-3 AC
* 2-3 BC
*/
double distancexAB = (x2 - x1) * (x2 - x1);
double distanceyAB = (y2 - y1) * (y2 - y1);
double distanceAB = csqrt(fabs(distancexAB - distanceyAB));
/*********************************************/
double distancexAC = (x3 - x1) * (x3 - x1);
double distanceyAC = (y3 - y1) * (y3 - y1);
double distanceAC = csqrt(fabs(distancexAC - distanceyAC));
/*********************************************/
double distancexBC = (x2 - x3) * (x2 - x3);
double distanceyBC = (y2 - y3) * (y2 - y3);
double distanceBC = csqrt(fabs(distancexBC - distanceyBC));
/*********************************************/
printf("\n AB %lf", distanceAB);
printf("\n AC %lf", distanceAC);
printf("\n BC %lf", distanceBC);
double perimeter = distanceAB+distanceAC+distanceBC;
printf("\n Perimeter: %lf", perimeter);
}
getch();
return 0;
}
Test results:
Enter x , y coordinates of first vertice!
1 1
Enter x , y coordinates of second vertice!
2 2
Enter x , y coordinates of third vertice!
3 3
mAB 1.000000
mAC 1.000000These points does not forms a triangle!!!!
Second test
Enter x , y coordinates of first vertice!
3.4 5.6
Enter x , y coordinates of second vertice!
1.2 3.4
Enter x , y coordinates of third vertice!
1.8 9.8
mAB 1.000000
mAC 0.380952
AB 0.000000
AC 3.883298
BC 6.371813
Perimeter: 10.255111
Calculate the distance between points by adding the sum of squares, not subtracting them. fabs() not needed. sqrt() is sufficient. #Alexander Daum.
Even better, use hypot().
The hypot functions compute the square root of the sum of the squares of x and y, without undue overflow or underflow. C11 §7.12.7.3 2
double distancexAB = (x2 - x1) * (x2 - x1);
double distanceyAB = (y2 - y1) * (y2 - y1);
// double distanceAB = csqrt(fabs(distancexAB - distanceyAB));
double distanceAB = sqrt(distancexAB + distanceyAB);
// or even more simple
double distanceAB = hypot(x2 - x1, y2 - y1);
OP uses weak code to detect if slopes are parallel. 2 problems:
fabs() loses the sign of the slope.
OP's method is subject to divide by 0.0.
// Alternative code:
double delta_x12 = x1 - x2;
double delta_y12 = y1 - y2;
double delta_x13 = x1 - x3;
double delta_y13 = y1 - y3;
if (delta_x12*delta_y13 == delta_y12*delta_x13) {
printf("These points do not form a triangle.");
}
If OP wants to calculate the area, code could use Heron's formula and use that to determine if "points do not form a triangle".
double a = hypot(x1 - x2, y1 - y2);
double b = hypot(x2 - x3, y2 - y3);
double c = hypot(x2 - x1, y3 - y1);
double perimeter = a + b + c;
double s /* semi-perimeter */ = perimeter/2;
double area2 = s*(s-a)*(s-b)*(s-c);
// due to small inaccuracies, area2 may be negative.
double area = area2 > 0.0 ? sqrt(area2) : 0.0;
if (area == 0) {
printf("These points do not form a triangle.");
}
Where calculating distances, you have to write:
double distanceAB = sqrt(distancexAB + distanceyAB);
instead of
double distanceAB = csqrt(fabs(distancexAB - distanceyAB));
Because the distance is the hypothenuse and use sqrt instead of csqrt, because csqrt is intended for complex variables.
You don't need fabs, because distancexAB and distanceyAB are squares and so they cannot be negative, and the sum of two positive numbers will be positive as long as no overflow occurs.
I have been writing this code for checking the euler angles and quaternions, but it is not run correcly (or maybe I do not understand the rotations):
#include <stdio.h>
#include <math.h>
#define DR2D (180 / M_PI)
#define DD2R (M_PI / 180)
int main(int argc, char** argv)
{
float x, y, z;
x = 0 * DD2R;
y = 0 * DD2R;
z = 180 * DD2R;
printf("x=%f y=%f z=%f\n", x, y, z);
float sx = sin(x / 2);
float sy = sin(y / 2);
float sz = sin(z / 2);
float cx = cos(x / 2);
float cy = cos(y / 2);
float cz = cos(z / 2);
float qx, qy, qz, qw;
printf("sx = %f sy = %f sz = %f cx = %f cy = %f cz = %f\n", sx, sy, sz, cx, cy, cy);
qx = cx*cy*sz + sx*sy*cz;
qy = sx*cy*cz + cx*sy*sz;
qz = cx*sy*cz - sx*cy*sz;
qw = cx*cy*cz - sx*sy*sz;
printf("Quaternion -> (%f, %f, %f, %f)\n", qx, qy , qz , qw);
//------------------------------------------------------------------
float sqw = qw*qw;
float sqx = qx*qx;
float sqy = qy*qy;
float sqz = qz*qz;
float unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
float test = qx*qy + qz*qw;
if (test > 0.499*unit) { // singularity at north pole
x = 2 * atan2(qx,qw);
y = M_PI/2;
z = 0;
}
else if (test < -0.499*unit) { // singularity at south pole
x = -2 * atan2(qx,qw);
y = -M_PI/2;
z = 0;
}
else {
x = atan2(2*qy*qw-2*qx*qz , sqx - sqy - sqz + sqw);
y = asin(2*test/unit);
z = atan2(2*qx*qw-2*qy*qz , -sqx + sqy - sqz + sqw);
}
printf("recover euler x=%.2f y=%.2f z=%.2f\n",
x * DR2D, y * DR2D, z * DR2D);
}
Because the output is very weird:
For example: x 180º y 90º z 90º
x=3.141593 y=1.570796 z=1.570796
sx = 1.000000 sy = 0.707107 sz = 0.707107 cx = -0.000000 cy = 0.707107 cz = 0.707107
Quaternion -> (0.500000, 0.500000, -0.500000, -0.500000)
reconversion euler x=270.00 y=90.00 z=0.00
Or for example x 90º y 90º z 90º
x=1.570796 y=1.570796 z=1.570796
sx = 0.707107 sy = 0.707107 sz = 0.707107 cx = 0.707107 cy = 0.707107 cz = 0.707107
Quaternion -> (0.707107, 0.707107, 0.000000, 0.000000)
recover euler x=180.00 y=90.00 z=0.00
The algorithm you use has a domain that lies in the interval [0,pi/2) only, the first quadrant. Or, because you want the input to be in degrees, between 0 (zero) inclusive and 90 degrees exclusive.