Star Program C++

63 views

Posted: 21 Jun 2021 (06:34)
Last Edited: 21 Jun 2021 (19:59)

Salil M.

@cmd05

...

Plotting n sided star using Graphical Interface

f3a086baa1cd611befa35b733c0d7cd1.pngStar.cpp


#include "Graph.h"
#include "Star.h"
#include <cmath>
#include <iostream>


const double PI = atan(1) * 4;

namespace Graph_lib {
	Star::Star(Point center, int n, int w, int len) {
		// outer circle
		std::vector<Point> out;
		std::vector<Point> in;


		for (int i = 0; i < n; i++)
			out.push_back({
			  center.x + int(len * cos(float(2 * PI * i) / n)),
			  center.y - int(len * sin(float(2 * PI * i) / n))
			});
		
		for (int i = 0; i < n; i++) {
			const double angle = float(2 * PI * i) / n;
			in.push_back({
			  center.x + int(w * cos(angle + PI/float(n))),
			  center.y - int(w * sin(angle + PI / float(n)))
			});
		}

		for (int i = 0; i < n; i++) {
			std::cout << out[i] << in[i];
			add(out[i]);
			add(in[i]);
		}
	}
};


Main.cpp

#include <stdexcept>
#include <string>
#include <vector>
#include "Simple_window.h"
#include "Graph.h"
#include "Star.h"

int main() {
	using namespace  Graph_lib;
	Graph_lib::Point tl{ 100, 100 }; // top left corner. Window position on screen
	Simple_window win{ tl, 600, 400, "Canvas" };

	Graph_lib::Star star{ {300, 200}, 100, 100, 200};
	star.set_fill_color(Color::cyan);
	win.attach(star);

	win.set_label("Canvas");
	win.wait_for_button(); // display to screen
}


Results:


7f8b10c4b51e6fe7dc029525d352dcff.png

Points


7ab4de37b6402d2b550dab6f93d98c8f.gif

0 to nth generation



9f042f261dec818317dc5501ea322bcf.gif

Nth to 0th Generation



7bbc6c9dbe45db492e550e1d37cc8982.png

100 sided star




08401168de5369a7ee7632473539916b.png

5 sided star




Tags


gui cpp programming geometry graphics

Comments





Salil M. 24 Aug 2021 (12:01)  (edited)

https://github.com/cmd3BOT/Practice-and-Principles-CPP-Solutions/tree/master/chap13/ex_19/ex_19

REPLY