Creating circle with node points

Hello Everyone , i’m super new to programming, but i could use your help in solving an issue.I have come across a problem where I have to write a code in cpp to draw a circle with nodes at the angle increment of 30. But flexible enough to work if I change my mind to 15 degree increment. The circle is connected to a hollow tube with inner radius. the center lies at the middle of U shape tube and circles are the top end of U.

Thanks in advance. your help is much appreciated.

why are you coding that “by hand” ( copy-pasting each line and do slight modifications)?
You should do it inside a for loop, which will give you a lot more flexibility.
Can you post a drawing of what you want to achieve? and also paste the code here, as text, not as a screenshot.

Hi, Thanks for the reply. Here is the model that needs to be created in C++.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include “MyProfile.h”
#include “Element.h”
#include “Node.h”

#define PI 3.14159265359

// constructor
MyProfile::MyProfile(const char* pName,
double hr, double br, double tr,double hL, double bL, double tL, double d0,double di) :
// copy input data
this->hr = hr;
this->br = br;
this->tr = tr;
this->hL = hL;
this->bL = bL;
this->tL = tL;
this->d0 = d0;
this->di = di;

// check the data


// create the profile

//double d0
//double di
//double tT =(d0 - di)/2.; //tube thickness.

// Check the profile’s data
int MyProfile::Check()
double dEps = 0.5;
double tT =(d0 - di)/2.; //tube thickness.
if (hr < 3.*tr) throw “error: h of Hollow Rectangle Profile invalid!”;
if (br < 3.*tr) throw “error: b of Hollow Rectangle Profile invalid!”;
if (tr < dEps) throw “error: t of Hollow Rectangle Profile invalid!”;
if (hL < 3.*tL) throw “error: h of L Profile invalid!”;
if (bL < 3.*tL) throw “error: b of L Profile invalid!”;
if (tL < dEps) throw “error: t of L Profile invalid!”;
if (d0 < 3.*tT) throw “error: outer diameter of Circle Profile invalid!”;
if (di < 3.*tT) throw “error: inner diameter of Circle Profile invalid”;
return 1;

// create the geometry
int MyProfile::Create()
// add node and element space
AddNodeContainer(32); // For the number of nodes = 32
AddElementContainer(32); // For the number of elements = 32

double dR = hr/2. - tr/2. ;
double dL = (bL - (tL/2.));
double dK = (hL - (tL/2.));
double tT =(d0 - di)/2.;    //tube thickness.
double tube_r = (di + tT)/2.;
double c_x = (hr/2.) + dL ;     // X cordinate of tube centre
double c_y =  dK + tube_r;          // Y cordinate of tube centre

// creation of the nodes
Node* pN[32];
pN[0]   = new Node( 1,       -dR,         0);
pN[1]   = new Node( 2,        dR,         0);
pN[2]   = new Node( 3,       -dR,          (br - tr));
pN[3]   = new Node( 4,       dR,         -(br - tr));
pN[4]   = new Node( 5,      -hr/2.,             0);
pN[5]   = new Node( 6,      -((hr/2)+dL),        0);
pN[6]   = new Node( 7,      -(c_x + (tube_r*cos(270*(PI/180.)))),   c_y + (tube_r*sin(270*(PI/180.))));
pN[7]   = new Node( 8,      -(c_x + (tube_r*cos(300*(PI/180.)))),   c_y + (tube_r*sin(300*(PI/180.))));
pN[8]   = new Node( 9,      -(c_x + (tube_r*cos(330*(PI/180.)))),   c_y + (tube_r*sin(330*(PI/180.))));
pN[9]   = new Node(10,       -(c_x + (tube_r*cos(0*(PI/180.)))),   c_y + (tube_r*sin(0*(PI/180.))));
pN[10]   = new Node(11,      -(c_x + (tube_r*cos(30*(PI/180.)))),   c_y + (tube_r*sin(30*(PI/180.))));
pN[11]   = new Node(12,      -(c_x + (tube_r*cos(60*(PI/180.)))),   c_y + (tube_r*sin(60*(PI/180.))));
pN[12]   = new Node(13,      -(c_x + (tube_r*cos(90*(PI/180.)))),   c_y + (tube_r*sin(90*(PI/180.))));
pN[13]   = new Node(14,      -(c_x + (tube_r*cos(120*(PI/180.)))),   c_y + (tube_r*sin(120*(PI/180.))));
pN[14]   = new Node(15,      -(c_x + (tube_r*cos(150*(PI/180.)))),   c_y + (tube_r*sin(150*(PI/180.))));
pN[15]   = new Node(16,      -(c_x + (tube_r*cos(180*(PI/180.)))),   c_y + (tube_r*sin(180*(PI/180.))));
pN[16]   = new Node(17,      -(c_x + (tube_r*cos(210*(PI/180.)))),   c_y + (tube_r*sin(210*(PI/180.))));
pN[17]   = new Node(18 ,      -(c_x + (tube_r*cos(240*(PI/180.)))),   c_y + (tube_r*sin(240*(PI/180.))));
pN[18]   = new Node(19,       hr/2.,         0);
pN[19]   = new Node(20,      ((hr/2)+dL),         0);
pN[20]   = new Node(21,      (c_x + (tube_r*cos(270*(PI/180.)))),   c_y + (tube_r*sin(270*(PI/180.))));
pN[21]   = new Node(22,       (c_x + (tube_r*cos(300*(PI/180.)))),   c_y + (tube_r*sin(300*(PI/180.))));
pN[22]   = new Node(23,       (c_x + (tube_r*cos(330*(PI/180.)))),   c_y + (tube_r*sin(330*(PI/180.))));
pN[23]   = new Node(24,     (c_x + (tube_r*cos(0*(PI/180.)))),   c_y + (tube_r*sin(0*(PI/180.))));
pN[24]   = new Node(25,      (c_x + (tube_r*cos(30*(PI/180.)))),   c_y + (tube_r*sin(30*(PI/180.))));
pN[25]   = new Node(26,      (c_x + (tube_r*cos(60*(PI/180.)))),   c_y + (tube_r*sin(60*(PI/180.))));
pN[26]   = new Node(27,       (c_x + (tube_r*cos(90*(PI/180.)))),   c_y + (tube_r*sin(90*(PI/180.))));
pN[27]   = new Node(28,      (c_x + (tube_r*cos(120*(PI/180.)))),   c_y + (tube_r*sin(120*(PI/180.))));
pN[28]   = new Node(29,      (c_x + (tube_r*cos(150*(PI/180.)))),   c_y + (tube_r*sin(150*(PI/180.))));
pN[29]   = new Node(30,      (c_x + (tube_r*cos(180*(PI/180.)))),   c_y + (tube_r*sin(180*(PI/180.))));
pN[30]   = new Node(31,       (c_x + (tube_r*cos(210*(PI/180.)))),   c_y + (tube_r*sin(210*(PI/180.))));
pN[31]   = new Node(32,      (c_x + (tube_r*cos(240*(PI/180.)))),   c_y + (tube_r*sin(240*(PI/180.))));

// creation of the elements
Element* pE[32];
pE[0]   = new Element(1, pN[0],  pN[1],  tr);
pE[1]   = new Element(2, pN[1],  pN[2],  tr);
pE[2]   = new Element(3, pN[2],  pN[3],  tr);
pE[3]   = new Element(4, pN[3],  pN[4],  tr);
pE[4]   = new Element(5, pN[0],  pN[5],  tL);
pE[5]   = new Element(6, pN[5],  pN[6],  tL);
pE[6]   = new Element(7, pN[6],  pN[7],  tT);
pE[7]   = new Element(8, pN[7],  pN[8],  tT);
pE[8]   = new Element(9, pN[8],  pN[9],  tT);
pE[9]   = new Element(10, pN[9], pN[10],  tT);
pE[10]   = new Element(11, pN[10],  pN[11],  tT);
pE[11]   = new Element(12, pN[11],  pN[12],  tT);
pE[12]   = new Element(13, pN[12],  pN[13],  tT);
pE[13]   = new Element(14, pN[13],  pN[14],  tT);
pE[14]   = new Element(15, pN[14],  pN[15],  tT);
pE[15]   = new Element(16, pN[15],  pN[16],  tT);
pE[16]   = new Element(17, pN[16],  pN[17],  tT);
pE[17]   = new Element(18, pN[6],   pN[17],  tT);
pE[18]   = new Element(19, pN[1],  pN[18],  tL);
pE[19]   = new Element(20, pN[18],  pN[19],  tL);
pE[20]   = new Element(21, pN[19],  pN[20],  tT);
pE[21]   = new Element(22, pN[20],  pN[21],  tT);
pE[22]   = new Element(23, pN[21],  pN[22],  tT);
pE[23]   = new Element(24, pN[22],  pN[23],  tT);
pE[24]   = new Element(25, pN[23],  pN[24],  tT);
pE[25]   = new Element(26, pN[24],  pN[25],  tT);
pE[26]   = new Element(27, pN[25],  pN[26],  tT);
pE[27]   = new Element(28, pN[26],  pN[27],  tT);
pE[28]   = new Element(29, pN[27],  pN[28],  tT);
pE[29]   = new Element(30, pN[28],  pN[29],  tT);
pE[30]   = new Element(31, pN[29],  pN[30],  tT);
pE[31]   = new Element(32, pN[19],  pN[30],  tT);

// add elements to the profile
for (int i=0;i<32;i++)   AddElement(pE[i]);

return 1;


// list all profile data
void MyProfile::List()
// list input data
sprintf(pMsg,“Profile name…: %s\n”,pName);
sprintf(pMsg," Hollow Rectangle Profile Length …: %10.2f mm\n",hr);
sprintf(pMsg," Hollow Rectangle Profile Width …: %10.2f mm\n",br);
sprintf(pMsg," Hollow Rectangle Profile Thickness…: %10.2f mm\n",tr);
sprintf(pMsg," L-profile height…: %10.2f mm\n",hL);
sprintf(pMsg," L-profile width…: %10.2f mm\n",bL);
sprintf(pMsg," L-profile thickness…: %10.2f mm\n",tL);
sprintf(pMsg," Outer diameter of the circle…: %10.2f mm\n",d0);
sprintf(pMsg," Inner diameter of the circle…: %10.2f mm\n",di);

// call the base class's methods!


I tried few instances to use loop but was unsuccesful. Thanks again. I appreciate the help.

something like the following code should work.

#pragma once

#include "ofMain.h"

class CircleNode: public ofNode
	CircleNode(ofNode& _parent, float _radius):radius(_radius)
	// this function overrides ofNode's function and it will apply the proper transformations.
	// what ever you draw inside this function is in the node's local coordinate system (its parent is at 0,0)
	virtual void customDraw() override
		ofDrawCircle(0,0, radius);
	float radius;

class CircleWithCircles: public CircleNode{
	void setup(float _radius, size_t numChildren, float childrenDistance, float angle)
		radius = _radius;
		for(size_t i = 0; i < numChildren; i++){
			circles.emplace_back(CircleNode(*this, _radius));
			circles.back().setPosition(0, childrenDistance, 0);
			circles.back().rotateAroundDeg(angle * i, {0,0,1}, {0,0,0});
	vector<CircleNode> circles;

class ofApp : public ofBaseApp{

		void setup();
		void draw();
	CircleWithCircles circle;


#include "ofApp.h"

void ofApp::setup(){
	circle.setup(20, 12, 200, 30);
	circle.setPosition(ofGetWidth()/2, ofGetHeight()/2, 0);

void ofApp::draw(){
	for(auto& c: circle.circles)

if you want to move the central circle just call setPosition and all the other circles will move accordingly

BTW, when you paste code here, please select it all and press the </> button that is in the text’s area top bar. This will format it all so it looks like code.

1 Like

Thanks for the response.
‘ofMain.h’ file , not found is the error i’m encountering at the moment.
Sorry if it was a stupid question

Create a new project with Project Generator and delete everything inside ofApp.h and ofApp.cpp
and copy/paste the code from @roymacdonald.

Not stupid at all. It sometimes happens. As @Dorald says, update or create a new project with Project Generator. That usually fixes this issue.