Area of a triangle
#include <stdio.h>
#include <math.h>
typedef struct {
float x;
float y;
} point;
typedef struct {
point ver1;
point ver2;
point ver3;
} triangle;
float area(triangle T) {
return(fabs((T.ver2.x - T.ver1.x)*(T.ver3.y-T.ver1.y) -
(T.ver2.y - T.ver1.y)*(T.ver3.x-T.ver1.x))/2);
}
int main() {
triangle T;
float a;
printf("The first point is: ");
scanf("%f %f",&T.ver1.x,&T.ver1.y);
printf("The second point is: ");
scanf("%f %f",&T.ver2.x,&T.ver2.y);
printf("The third point is: ");
scanf("%f %f",&T.ver3.x,&T.ver3.y);
a = area(T);
printf("The area of the triangle is %f.\n",a);
}
Volume of a tetrahedron
#include <stdio.h>
#include <math.h>
typedef struct {
float x;
float y;
float z;
} point;
typedef struct {
point ver1;
point ver2;
point ver3;
point ver4;
} tetrahedron;
point Diff(point a, point b) {
point c;
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
return(c);
}
point Cross(point a, point b) {
point c;
c.x = a.y*b.z - a.z*b.y;
c.y = a.z*b.x - a.x*b.z;
c.z = a.x*b.y - a.y*b.x;
return(c);
}
float Dot(point a, point b) {
return(a.x*b.x + a.y*b.y + a.z*b.z);
}
float Volume(tetrahedron t) {
point a, b, c;
a = Diff(t.ver3,t.ver1);
b = Diff(t.ver2,t.ver1);
c = Diff(t.ver4,t.ver1);
return(fabs(Dot(c,Cross(a,b)))/6);
}
int main(){
tetrahedron T;
float vol;
printf("The coordinates of the first point are: ");
scanf("%f %f %f", &T.ver1.x, &T.ver1.y, &T.ver1.z);
printf("The coordinates of the second point are: ");
scanf("%f %f %f", &T.ver2.x, &T.ver2.y, &T.ver2.z);
printf("The coordinates of the third point are: ");
scanf("%f %f %f", &T.ver3.x, &T.ver3.y, &T.ver3.z);
printf("The coordinates of the fourth point are: ");
scanf("%f %f %f", &T.ver4.x, &T.ver4.y, &T.ver4.z);
vol = Volume(T);
printf("The volume of the tetrahedron T is equal to %f\n", vol);
}