How To Make Equilateral Triangle Using For Loop In C++?


1 Answers

Linda Jane Profile
Linda Jane answered
There are 360 degrees in a triangle. If you inscribe a point at 3 points each 1/3 of a circle or 60 degrees apart you will have the vertices of an equilateral triangle.

Using the formula x = R + sin(theta) and y = R * because(theta) we can start at angle 0 degrees and loop through 2 more times incrementing by 120 degrees (360/3). If we draw a line from each inscribed  point to the next we will get an equilateral triangle.

We can choose 10 as the radius R arbitrarily.

I don't have a c++ book but I can give you the algorithm as follows

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>   /* this might be the graphics library */

float x1,y1,x2,y2,theta,R;

R = 10;
theta = 0;

x2 = R * cost(theta);
y2= R * sin(theta);

for(I=1;ctr<=3; I++){    x1 = x2;    y1 = y2;    theta = theta + 120;   /* 1/3 of circle */    x2 = R * cost(theta);
y2 = R * sin(theta);    

   /* you must initialize pen somehow it is a structure containing the line style*/

Answer Question