I am trying to define centroid in the following code as centroid is part of the private data for this class. But when I output the data it reads 0, 0 instead of the real values. Vec is a 3 dimension class that i have created. I'm not sure what could be wrong.. Help!!

Header file:

//////////////////////////////////////////////////
/// \class triangle
/// \brief triangle class
/// ///////////////////////////////////////////////
/// \file triangle.h
/// \brief provide a set fo functions to facilitate triangle manipulations
//////////////////////////////////////////////////

#include <math.h>
#include <iostream>
#include <fstream>
#include "list.h"
#include "vec.h"
#include "circle.h"
using namespace std;

#ifndef TRIANGLE_H
#define TRIANGLE_H

class triangle : public ListElement
{
public:

    friend ostream & operator << (ostream& out, const triangle& inTriangle);

    virtual double ElementValue(void);

    /// \brief Non-default Constructor
    triangle(const unsigned int id_in, const Vec iIn, const Vec jIn, const Vec kIn, 
             const Vec triNodeIDin);

    /// \brief Destructor
    ~triangle(){};

    /// \brief Calculate the centroid of a triangle
    Vec get_centroid(void){return centroid;}

    /// \brief Calculate the area of a triangle
    double get_area(void){return area;}

    /// \brief Prints out the triangle area
    virtual void print(void) {cout << "Tri area: " << ElementValue() << endl;}

    /// \brief Writes out the triangle data
    virtual void out(ofstream& myfile);

private:
    unsigned int id;
    Vec i; /// <First node of triangle
    Vec j; /// <Second node of triangle
    Vec k; /// <Third node of triangle
    Circle theCircle;
    Vec centroid;
    double area;
    Vec triNode;
};

#endif // TRIANGLE_H

The source file constructor:

triangle::triangle(unsigned int id_in, Vec iIn, Vec jIn, Vec kIn, Vec triNodeIDin): 
        id(id_in), i(iIn), j(jIn), k(kIn),theCircle(iIn, jIn, kIn), triNode(triNodeIDin){

    /// Method:
    /// -Takes in nothing
    /// -Calculate the area of a triangle using vectors
    /// \param void
    /// \return area of the triangle

    Vec vecA(i[0], i[1], 0);
    Vec vecB(j[0], j[1], 0);
    Vec vecC(k[0], k[1], 0);
    Vec vecAB = vecA-vecB;
    Vec vecAC = vecA-vecC;
    area = ((vecAB*vecAC).norm())*0.5;

    /// Method:
    /// -Takes in nothing
    /// -Compute the x, y coordinates of the centroid
    /// \param void
    /// \return centroid of a triangle

    double cX = (i[0]+j[0]+k[0])/3;
    double cY = (i[1]+j[1]+k[1])/3;
    double cZ = 0;
    centroid(cX, cY, cZ);
}

I think you'll have to share your Vec class and your display code to get the proper help. Maybe someone else can figure something out from this, but I can't.

The call to the centroid constructor on Line 26 concerns me. It probably has something to do with that, but I'm not sure.

Edited 6 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.