d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need A Faster Way To Do This > C++
Add Reply New Topic New Poll
Member
Posts: 25,431
Joined: Apr 3 2008
Gold: 10.27
Jan 30 2015 10:17am
I have 2 questions.

1) How can I create circles more quickly without having to retype all of the code for each spawning circle?

2) Why are my spiraling circles not smooth? Is it because they are moving too quickly?

DO NOT COMPILE MY PROGRAM IF YOU SUFFER FROM ANY FORM OF SEIZURES!!! I am not responsible for what happens.

Here's the code:

Code
#include <Windows.h>
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <math.h>

using namespace std;

sf::CircleShape Circle1(50);
sf::CircleShape Circle2(50);
sf::CircleShape Circle3(50);

sf::ContextSettings settings;

void circthree();
bool bcircthree = false;

void circtwo();
bool bcirctwo = false;
bool bwinopen = true;

int I, P, SizeTwo, angleTwo, runTwo, SizeThree, angleThree, runThree, J, K;

int main()
{
FreeConsole();

settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(900, 900), "Screensaver 1", sf::Style::Default, settings);

Circle1.setFillColor(sf::Color::Blue);
Circle1.setOrigin(50, 50);

Circle2.setFillColor(sf::Color::Red);
Circle2.setOrigin(50, 50);

Circle3.setFillColor(sf::Color::Blue);
Circle3.setOrigin(50, 50);

int originX, originY, angle, Size, run, X, Y;

originX = 50;
originY = 50;
angle = 45;
Size = 50;
run = 1;
SizeTwo = 50;
angleTwo = 45;
SizeThree = 50;
angleThree = 45;

sf::Thread CTwo(&circtwo);
CTwo.launch();

sf::Thread CThree(&circthree);
CThree.launch();

while (window.isOpen())
{
X = Circle1.getPosition().x + sin(angle)*Size;
Y = Circle1.getPosition().y + cos(angle)*Size;



sf::Event event;
while (window.pollEvent(event))
if (event.type == sf::Event::Closed){
bwinopen = false;
window.close();

}
if (run % 2 == 0)
{

window.clear(sf::Color::Red);
}
else
{
window.clear(sf::Color::Blue);
}

window.draw(Circle1);



if (bcirctwo == true)
{
window.draw(Circle2);
}

if (bcircthree == true)
{
window.draw(Circle3);
}

if (run == 1)
{
Circle1.setPosition(450, 450);
}
else
{
Circle1.setPosition(X, Y);
Sleep(100);
}

window.display();

angle++;
Size = Size + 1;

run++;
}
return 0;
}

void circtwo()
{
Sleep(3000);
bcirctwo = true;

while (bwinopen == true)

{
if (runTwo == 1)
{
Circle2.setPosition(450, 450);
}
else
{
Circle2.setPosition(I, P);
Sleep(100);
}

I = Circle2.getPosition().x + sin(angleTwo)*SizeTwo;
P = Circle2.getPosition().y + cos(angleTwo)*SizeTwo;

angleTwo++;

SizeTwo = SizeTwo + 1;

runTwo++;
}
}

void circthree()
{
Sleep(6000);
bcircthree = true;

while (bwinopen == true)

{
if (runThree == 1)
{
Circle3.setPosition(450, 450);
}
else
{
Circle3.setPosition(J, K);
Sleep(100);
}

J = Circle3.getPosition().x + sin(angleThree)*SizeThree;
K = Circle3.getPosition().y + cos(angleThree)*SizeThree;

angleThree++;

SizeThree = SizeThree + 1;

runThree++;
}
}
Member
Posts: 18
Joined: Jan 6 2015
Gold: Locked
Trader: Scammer
Jan 30 2015 03:13pm
I can't compile your code.
Member
Posts: 25,431
Joined: Apr 3 2008
Gold: 10.27
Jan 30 2015 03:28pm
Quote (MisterTrain @ Jan 30 2015 03:13pm)
I can't compile your code.


If it is because you suffer from seizures then I can't help ya lol.

If you can't compile it make sure you have sfml properly installed. And the right version of 2.2.

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 30 2015 03:58pm
Quote
1) How can I create circles more quickly without having to retype all of the code for each spawning circle?


ctrl c, ctrl v

or refactor into a function that can create circles then call that function for each circle you want.
Member
Posts: 25,431
Joined: Apr 3 2008
Gold: 10.27
Jan 30 2015 04:18pm
Quote (carteblanche @ Jan 30 2015 03:58pm)
ctrl c, ctrl v

or refactor into a function that can create circles then call that function for each circle you want.


I'm aware of copy and paste lol...

Even doing that takes a good amount of time. You have to rename each circle to a new number.


Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Jan 30 2015 05:30pm
Avoid repetition, create a general function for generating circles that takes the parameters you need, size, position or whatever and reuse that.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 30 2015 05:32pm
Quote (EBAZ @ Jan 30 2015 05:18pm)
I'm aware of copy and paste lol...

Even doing that takes a good amount of time. You have to rename each circle to a new number.


Without looking at any of your code:

Code
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

class Circle
{
public:
Circle(std::string name, double radius) : _name(name), _radius(radius){}
std::string getName() { return _name; }
double getRadius() { return _radius; }
virtual ~Circle() { }
private:
std::string _name;
double _radius;

};

int main()
{
std::vector<Circle> circles;

for(int i = 0; i < 4; i++)
{
double r;
std::cin >> r;
std::stringstream sstm;
sstm << "Circle" << i;
circles.push_back(Circle(sstm.str(),r));
}
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll