How to use the proper cross product? - c

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

Related

I have just started learning function pointers in C and and was trying to code a program but there seems to be a problem and I can't understand what?

So, I was trying to code a program which asks the user to input the points of a graph to calculate the euclidean distance and uses it as a radius to give the area of a circle.
Here's my code:
/*You have to take four points(x1,y1,x2,y2) from the user and use it radius to find area of a circle. To find the distance between these points, you will use the Euclidean distance formula.*/
#include <stdio.h>
#include <math.h>
#define PI 3.14
float euclideanDistance(float x1, float x2, float y1, float y2)
{
float ed = 0;
ed = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
return ed;
}
int areaOfCircle(float x1, float y1, float x2, float y2, float(ed)(float x1, float y1, float x2, float y2))
{
return PI * (*ed)(x1, y1, x2, y2) * (*ed)(x1, y1, x2, y2);
//not sure if it's correct or not. No error squiggles.
}
int main()
{
float x1, y1, x2, y2;
float (*fptr)(float, float, float, float);
fptr = euclideanDistance;
printf("Enter the four points x1,y1,x2,y2 to calculate the Euclidean Distance.\n");
printf("x1:");
scanf("%f", &x1);
printf("y1:");
scanf("%f", &y1);
printf("x2:");
scanf("%f", &x2);
printf("y2:");
scanf("%f", &y2);
;
printf("The euclidean distance is %f", fptr(x1, x2, y1, y2));
printf("The area of the circle which has the above mentioned Euclidean Distance as it's radius is: %f", areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2))); //error in this printf.
return 0;
}
There are two problems over here.
I am not getting how to use the function euclideanDistance as a radius in areaOfCircle and second is how to implement it in my main function.
For the second problem In the VS Code it's showing me the error that.
{"message": "argument of type "float" is incompatible with parameter of type "float (*)(float x1, float y1, float x2, float y2)""}
Please explain what I'm doing wrong and guide me.
The problem is that you're basically double-calling euclideanDistance. In main, you do areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2)), and then in areaOfCircle, you do (*ed)(x1, y1, x2, y2). To pass a function pointer, you just pass it like any other kind of pointer, rather than calling it with arguments. In other words, change areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2)) to areaOfCircle(x1, x2, y1, y1, fptr).

Finding Third Coordinate Of a Triangle

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

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

Sides of triangle based on coordinates in 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 ...

calculate length and perimeter of triangle from points

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.

Resources