vivichrist
April 22nd, 2011, 11:31 PM
I think I maybe doing recursion wrong...?
main.cc
#include "myarea.h"
#include <gtkmm/main.h>
#include <gtkmm/window.h>
int main(int argc, char** argv)
{
Gtk::Main kit(argc, argv);
Gtk::Window win;
win.set_title("DrawingArea");
MyArea area;
win.add(area);
area.show();
Gtk::Main::run(win);
return 0;
}
myarea.h
#ifndef GTKMM_EXAMPLE_MYAREA_H
#define GTKMM_EXAMPLE_MYAREA_H
#include <vector>
#include <gtkmm/drawingarea.h>
#include "linesegment.h"
class MyArea : public Gtk::DrawingArea
{
double sx, sy, length;
double branchAngle;
double shrinkage;
int numBranches;
std::vector<LineSegment> lines;
public:
MyArea();
virtual ~MyArea();
protected:
//Override default signal handler:
void make_tree(double nx, double ny, double dist, double ang, int branches, double propor);
virtual bool on_expose_event(GdkEventExpose* event);
};
#endif // GTKMM_EXAMPLE_MYAREA_H
myarea.cc
#include "myarea.h"
#include <cmath>
#include <stdlib.h>
#include <cairomm/context.h>
using namespace std;
MyArea::MyArea(){
branchAngle = M_PI*0.3333333333*(0.1+rand()*0.000012207);
shrinkage = 0.5 + rand()*0.0000030518;
numBranches = 5+rand()*0.000091553;
sx = (double)(rand()%900);
sy = (double)(400.0 + rand()%100);
length = (double)(50 +(rand()%150));
LineSegment line(sx, sy, sx, sy-length);
lines.push_back(line);
make_tree(sx, sy, length, -M_PI*0.5, 2, 1.0);
}
MyArea::~MyArea()
{
}
bool MyArea::on_expose_event(GdkEventExpose* event)
{
// This is where we draw on the window
Glib::RefPtr<Gdk::Window> window = get_window();
if(window)
{
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
// generate a tree coordinates from random center of the window
Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
// clip to the area indicated by the expose event so that we only redraw
// the portion of the window that needs to be redrawn
cr->rectangle(event->area.x, event->area.y,
event->area.width, event->area.height);
cr->clip();
// draw red lines out from the center of the window
cr->set_source_rgb(0.8, 0.0, 0.0);
for(int i=0;i<(int)lines.size();i++){
cr->set_line_width(sqrt((lines[i].fx-lines[i].fy)+(lines[i].tx-lines[i].ty))*0.05);
cr->move_to(lines[i].fx, lines[i].fy);
cr->line_to(lines[i].tx, lines[i].ty);
cr->stroke();
}
}
return true;
}
void MyArea::make_tree(double nx, double ny, double dist, double ang, int branches, double propor){
// double rnddist = dist*((1-branches*0.1)*rand()+(branches*0.1));
double newx = nx + dist*cos(ang);
double newy = ny + dist*sin(ang);
int cap = fmin(branches, numBranches);
if (dist>3+(5.0/length))
for (int i=1;i<=cap;i++)
make_tree(newx, newy, dist*shrinkage, ang-(branchAngle*propor*i*(i%2>0?-1:1)), branches+1, propor);
};
main.cc
#include "myarea.h"
#include <gtkmm/main.h>
#include <gtkmm/window.h>
int main(int argc, char** argv)
{
Gtk::Main kit(argc, argv);
Gtk::Window win;
win.set_title("DrawingArea");
MyArea area;
win.add(area);
area.show();
Gtk::Main::run(win);
return 0;
}
myarea.h
#ifndef GTKMM_EXAMPLE_MYAREA_H
#define GTKMM_EXAMPLE_MYAREA_H
#include <vector>
#include <gtkmm/drawingarea.h>
#include "linesegment.h"
class MyArea : public Gtk::DrawingArea
{
double sx, sy, length;
double branchAngle;
double shrinkage;
int numBranches;
std::vector<LineSegment> lines;
public:
MyArea();
virtual ~MyArea();
protected:
//Override default signal handler:
void make_tree(double nx, double ny, double dist, double ang, int branches, double propor);
virtual bool on_expose_event(GdkEventExpose* event);
};
#endif // GTKMM_EXAMPLE_MYAREA_H
myarea.cc
#include "myarea.h"
#include <cmath>
#include <stdlib.h>
#include <cairomm/context.h>
using namespace std;
MyArea::MyArea(){
branchAngle = M_PI*0.3333333333*(0.1+rand()*0.000012207);
shrinkage = 0.5 + rand()*0.0000030518;
numBranches = 5+rand()*0.000091553;
sx = (double)(rand()%900);
sy = (double)(400.0 + rand()%100);
length = (double)(50 +(rand()%150));
LineSegment line(sx, sy, sx, sy-length);
lines.push_back(line);
make_tree(sx, sy, length, -M_PI*0.5, 2, 1.0);
}
MyArea::~MyArea()
{
}
bool MyArea::on_expose_event(GdkEventExpose* event)
{
// This is where we draw on the window
Glib::RefPtr<Gdk::Window> window = get_window();
if(window)
{
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
// generate a tree coordinates from random center of the window
Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
// clip to the area indicated by the expose event so that we only redraw
// the portion of the window that needs to be redrawn
cr->rectangle(event->area.x, event->area.y,
event->area.width, event->area.height);
cr->clip();
// draw red lines out from the center of the window
cr->set_source_rgb(0.8, 0.0, 0.0);
for(int i=0;i<(int)lines.size();i++){
cr->set_line_width(sqrt((lines[i].fx-lines[i].fy)+(lines[i].tx-lines[i].ty))*0.05);
cr->move_to(lines[i].fx, lines[i].fy);
cr->line_to(lines[i].tx, lines[i].ty);
cr->stroke();
}
}
return true;
}
void MyArea::make_tree(double nx, double ny, double dist, double ang, int branches, double propor){
// double rnddist = dist*((1-branches*0.1)*rand()+(branches*0.1));
double newx = nx + dist*cos(ang);
double newy = ny + dist*sin(ang);
int cap = fmin(branches, numBranches);
if (dist>3+(5.0/length))
for (int i=1;i<=cap;i++)
make_tree(newx, newy, dist*shrinkage, ang-(branchAngle*propor*i*(i%2>0?-1:1)), branches+1, propor);
};