Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:42:12+00:00 2026-05-30T02:42:12+00:00

I am very new to C++ and I am trying to implement a TriangleDynamic

  • 0

I am very new to C++ and I am trying to implement a TriangleDynamic object that can recursively split itself using a function called splitTriangleProject. It splits itself into four smaller TriangleDynamic objects (and projects the vertices of the new triangles onto a sphere with a given radius and origin, but I believe this is beside the point), pushes the newly created triangles into a vector that is part of the member data of the original object. The vector member data is called subTriangles. Then each subTriangle recalls the splitTriangleProject function until a certain “level” of splits has occurred.

The issue I am having is that only the first level of splitting is actually properly pushing to the subTriangles vector. I am certain that the issue has to do with the vectors going out of scope, or maybe the TriangleDynamic going out of scope. I imagine there is some solution with pointers. If anyone could help, it would be much appreciated.

Here is my TriangleDynamic declaration:

class TriangleDynamic
{
public:
    TriangleDynamic(const Point &P1, const Point &P2, const Point &P3);
    TriangleDynamic();
    ~TriangleDynamic(){}
    void printTriangle();
    void splitTriangleProject( int currentLevel, int maxLevel, const Point &org, double radius);
    void init();
    bool operator<(const TriangleDynamic&);
    bool operator>(const TriangleDynamic&);
    Line edge1;
    Line edge2;
    Line edge3;

    Point p1;
    Point p2;
    Point p3;

    Vect normal;

    bool lowestLevel;

    vector<TriangleDynamic> subTriangles;
    static int numTriangles;
    int triangleId;
};

int TriangleDynamic::numTriangles = 0;

and the constructors:

// constructor for the TriangleDynamic object
TriangleDynamic::TriangleDynamic(const Point &P1, const Point &P2, const Point &P3)
{
    p1 = P1;
    p2 = P2;
    p3 = P3;
    init();
}

TriangleDynamic::TriangleDynamic()
{
    p1 = Point(0,0,0);
    p2 = Point(0,0,1);
    p3 = Point(0,1,0);
    init();
}

void TriangleDynamic::init()
{
    edge1 = Line(p1,p2);
    edge2 = Line(p2,p3);
    edge3 = Line(p3,p1);

    Vect U = p2.minus( p1);
    Vect V = p3.minus(p1);

    normal = U.cross(V);

    lowestLevel = true;
    triangleId = numTriangles + 1;
    numTriangles = triangleId;
}

Here is my splitTriangleProject function:

void TriangleDynamic::splitTriangleProject(int currentLevel, int maxLevel, const Point &org, double radius)
{
    if ( currentLevel < maxLevel)
    {
        lowestLevel = false;
        Point worldOrigin = Point(0,0,0);
        double edge1MidMag = (edge1.midpoint - org).distance(worldOrigin) ;
        double edge2MidMag = (edge2.midpoint - org).distance(worldOrigin) ;
        double edge3MidMag = (edge3.midpoint - org).distance(worldOrigin) ;

        Point newEdge1Mid = (((edge1.midpoint) * radius )/ edge1MidMag) + org;
        Point newEdge2Mid = (((edge2.midpoint) * radius )/ edge2MidMag) + org;
        Point newEdge3Mid = (((edge3.midpoint) * radius )/ edge3MidMag) + org;

        TriangleDynamic t1(p1 , newEdge1Mid , newEdge3Mid);
        subTriangles.push_back(t1);

        TriangleDynamic t2(newEdge1Mid, p2, newEdge2Mid);
        subTriangles.push_back(t2);

        TriangleDynamic t3(newEdge3Mid, newEdge2Mid, p3);
        subTriangles.push_back(t3);

        TriangleDynamic t4(newEdge1Mid, newEdge2Mid, newEdge3Mid);
        subTriangles.push_back(t4);

        t1.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
        t2.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
        t3.splitTriangleProject( currentLevel + 1, maxLevel, org, radius); 
        t4.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
    }
}

Here is a function for recursively printing the triangleDynamic when it’s done:

void TriangleDynamic::printTriangle()
{
    cout<< "p1";
    p1.printPoint();
    cout << "\np2";
    p2.printPoint();
    cout << "\np3";
    p3.printPoint();
    cout << "\n\n";

    if(!lowestLevel)
    {
        int ctr;
        for (ctr=0; ctr<=subTriangles.size()-1; ctr++)
        {
            cout << "subTriangle\n";
            subTriangles[ctr].printTriangle();
        }
    }
}

Here is how I call it in my main:

int main()
{
    TriangleDynamic t = TriangleDynamic();
    t.splitTriangleProject(0,3,Point(), 1);
    t.printTriangle();
    cin.get();
    return 0;
}

I think I’ve posted too much at this point. Any help is appreciated, thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:42:13+00:00Added an answer on May 30, 2026 at 2:42 am

    The issue is right here

        TriangleDynamic t1(p1 , newEdge1Mid , newEdge3Mid);
        subTriangles.push_back(t1);
    
        TriangleDynamic t2(newEdge1Mid, p2, newEdge2Mid);
        subTriangles.push_back(t2);
    
        TriangleDynamic t3(newEdge3Mid, newEdge2Mid, p3);
        subTriangles.push_back(t3);
    
        TriangleDynamic t4(newEdge1Mid, newEdge2Mid, newEdge3Mid);
        subTriangles.push_back(t4);
    
        t1.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
        t2.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
        t3.splitTriangleProject( currentLevel + 1, maxLevel, org, radius); 
        t4.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
    

    Note how you push a copy of the triangle into the subTriangles vector before you call splitTrangleProject.

    Thus the triangles in the vector did not have splitTriangleProject called on them.

    Move the pushbacks to the end of the code and it should work.

        TriangleDynamic t1(p1 , newEdge1Mid , newEdge3Mid);
    
    
        TriangleDynamic t2(newEdge1Mid, p2, newEdge2Mid);
    
    
        TriangleDynamic t3(newEdge3Mid, newEdge2Mid, p3);
    
    
        TriangleDynamic t4(newEdge1Mid, newEdge2Mid, newEdge3Mid);
    
    
        t1.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
        t2.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
        t3.splitTriangleProject( currentLevel + 1, maxLevel, org, radius); 
        t4.splitTriangleProject( currentLevel + 1, maxLevel, org, radius);
    
        subTriangles.push_back(t3);
        subTriangles.push_back(t2);
        subTriangles.push_back(t1);
        subTriangles.push_back(t4);
    

    (Also, on another note, if this code starts getting slow it could be greatly sped up with a C++11 std::move. Don’t go into pointers unless you have to.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to generics and trying to implement it. How can i
I am very new to NTLM/LDAP and trying to authenticate using NTML running on
I'm very new to programming and I am trying to write a program that
I'm very new to RoR, and am trying to implement a user login system.
I'm very new to the AJAX and Javascript world and I'm trying to implement
I am very new to OOP styled PHP, and I am trying to implement
I'm trying to implement a suspend event that transitions the object to the :suspended
I'm trying to implement a very simple WCF service that returns JSON. I'm trying
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
I am very new to erlang and am trying to get my head around

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.