Jan 8, 2015 at 2:49pm UTC
Hello everyone!
I have to write some cpp program which computes area of a triangle using cross product,we give 3 vertices as R2 and 3 edges as double.
I am beginning like this;
#include <iostream>
#include "R2.h"
#include <cmath>
using namespace std;
double area ( R2 *A,double *z)
{ double aire;
R2 u0=A[1]-A[0];
R2 u1=A[2]-A[0];
int r[0]=u0.y*z[1]-z[0]*u1.y;
int r[1]=z[0]*u1.x-u0.x*z[1];
int r[2]=u0.x*u1.y-u0.y*u1.x;
}
what do you think? I am really bad.thank you!
Jan 8, 2015 at 4:21pm UTC
of course,
#include <cmath>
#include <cassert>
#include <cstdlib>
#include <iostream>
typedef double R;
// The class R2
class R2 {
public:
R x,y;
R2 () :x(0.),y(0.) {}
R2 (R a,R b):x(a),y(b) {}
R2 (const R2 & a,const R2 & b):x(b.x-a.x),y(b.y-a.y) {}
R2 (const R2 & a) :x(a.x),y(a.y) { cout << " const par copy " << endl;}
R2 ( R2 & a) :x(a.x),y(a.y) { cout << " const pas const par copy " << endl; }
R2 & operator=(const R2 & P) {x = P.x;y = P.y;return *this;}
R2 & operator+=(const R2 & P) {x += P.x;y += P.y;return *this;}
R2 & operator-=(const R2 & P) {x -= P.x;y -= P.y;return *this;}
R2 operator+(const R2 & P)const {return R2(x+P.x,y+P.y);}
R2 operator-(const R2 & P)const {return R2(x-P.x,y-P.y);}
R operator,(const R2 & P)const {return x*P.x+y*P.y;}
R operator^(const R2 & P)const {return x*P.y-y*P.x;}
R2 operator*(R c)const {return R2(x*c,y*c);}
R2 operator/(R c)const {return R2(x/c,y/c);}
R2 operator-()const {return R2(-x,-y);}
const R2 & operator+()const {return *this;}
R2 perp() const {return R2(-y,x);} // la perpendiculaire
R & operator[](int i) { if(i==0) return x; else if (i==1) return y; else {assert(0);exit(1);} ;}
const R & operator[](int i) const { if(i==0) return x; else if (i==1) return y; else {assert(0);exit(1);} ;}
R & operator()(int i) { if(i==1) return x; else if (i==2) return y; else {assert(0);abort();} ;}
const R & operator()(int i) const { if(i==1) return x; else if (i==2) return y; else {assert(0);abort();} ;}
R norme() const { return std::sqrt(x*x+y*y);}
static const int d=2;
};
inline std::ostream& operator <<(std::ostream& f, const R2 & P )
{ f << P.x << ' ' << P.y ; return f; }
inline std::istream& operator >>(std::istream& f, R2 & P)
{ f >> P.x >> P.y ; return f; }
inline R2 operator*(R c,const R2 & P) {return P*c;}
inline R2 perp(const R2 & P) { return R2(-P.y,P.x) ; }
//inline R2 Perp(const R2 & P) { return P.perp(); }
Jan 8, 2015 at 4:43pm UTC
> using cross product,we give 3 vertices and 3 edges
¿edges? ¿what for?
About your code, perhaps you want to return
something
Jan 8, 2015 at 8:35pm UTC
i want to compute the area of triangle ABC in R3 but we have 3 vertices like
A(x,y),B(x,y),C(x,y) with z0,z1,z2 3 edges of triangles.
we have to use (A,z0),(B,z1),(C,z2) and use cross product with that.
the formula that i know is 0.5(AB*AC) (cross product) but here in r2
Jan 8, 2015 at 9:37pm UTC
> we have to use (A,z0),(B,z1),(C,z2)
iirc, you've got points in R3. I don't understand why you call "edge" to the z component.
cross product gives you a vector, whose norm is 2*area of triangle. When working on R2, the result vector has the form (0, 0, z) and the its norm is z.
1 2 3 4 5 6 7 8
// w_j = e_{jkl} u_k v_l
w_1 = u_2 v_3 - u_3 v_2
w_2 = - u_1 v_3 + u_3 v_1
w_3 = u_1 v_2 - u_2 v_1
//|w|^2 = w_j w_j
norm = w_1*w1 + w_2*w_2 + w_3*w_3
return sqrt(norm)/2
I don't see you using z2, ¿why is 'r[x]' int?, and you are not returning anything.
Last edited on Jan 8, 2015 at 9:38pm UTC