For-Loop分段 - 过度运行(i <SIZE,但i = SIZE)

时间:2014-11-10 17:55:00

标签: c++ linux user-interface for-loop gnome

这是复制Sierpinski垫片的程序中的一项功能。该函数应该附加分形三角形中的点。

经过深思熟虑,我已经找到了问题所在:

void add_pts(int &x, int &y)
{ 
    Vector_ref<Rectangle> pt;
    for (int i = 0; i < POINTS; ++i)
    {
         pt_test; //generates changing x, y vals within the limits of the triangle
         cout << "pass" << i <<endl;
         pt.push_back(new Rectangle(Point(x,y),5,5));
         pt[i].set_fill_color(Color::yellow);
         win.attach(pt[i]); 
    }
}

输出为“pass1 ... pass [POINTS-1]”,但无论出于何种原因,当i = POINTS时它会运行并进入分段错误。我不知道为什么。有人可以帮忙吗?

这是我的代码。 pt_test和coord有点草率但看到它无法正常运行,很难确定我可以简化的内容。

#include <stdlib.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <time.h>
#include "Simple_window.h"
#include "Graph.h"
#include "Point.h"
#include "GUI.h"
#include "Window.h"
using namespace std;
using namespace Graph_lib;

// globals

const int POINTS = 5000;
unsigned int seed = (unsigned int)time(0);
Simple_window win(Point(100,100),1100,700,"Homework 9");

// function declarations

double random(unsigned int &seed);
bool coords(int &x, int &y);
void pt_test(int x, int y);
void add_pts(int &x, int &y);

int main()
{

    int x, y;

    // title

    Text title(Point(400,50), "The Sierpinski Gasket");
    title.set_font(Graph_lib::Font::helvetica_bold);
    title.set_font_size(25);
    title.set_color(Color::cyan);
    win.attach(title);

    // triangle

    Closed_polyline tri;
    tri.add(Point(250,75)); // A
    tri.add(Point(850,75)); // B
    tri.add(Point(550,675)); // C
    tri.set_fill_color(Color::white);
    tri.set_color(Color::dark_red);
    tri.set_style(Line_style(Line_style::solid,3));
    win.attach(tri);

    // vertices
    Text vert_a(Point(225,70), "A (250, 75)");
    vert_a.set_font(Graph_lib::Font::helvetica_bold);
    vert_a.set_font_size(15);
    vert_a.set_color(Color::cyan);
    Text vert_b(Point(855,70), "B (850, 75)");
    vert_b.set_font(Graph_lib::Font::helvetica_bold);
    vert_b.set_font_size(15);
    vert_b.set_color(Color::cyan);
    Text vert_c(Point(575,670), "C (550, 675)");
    vert_c.set_font(Graph_lib::Font::helvetica_bold);
    vert_c.set_font_size(15);
    vert_c.set_color(Color::cyan);

    win.attach(vert_a);
    win.attach(vert_b);
    win.attach(vert_c);

    // point selection

    add_pts(x, y);

    // window title and display

    win.wait_for_button();

}

double random(unsigned int &seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
    return double(seed)/double(MODULUS);
}

bool coords(int &x, int &y) // generates the points
{
    x = int(251 + 600*random(seed));
    y = int(76 + 600*random(seed));
    if( y > (2*x-425) && x<= 550 || x>=550 && y < (-2*x + 1775))
        return true;
}

void pt_test(int x, int y) // tests the points until they are within the range
{
    coords;
    while(coords == 0)
        coords;
}

void add_pts(int &x, int &y) // attaches the points as shapes
{ 
    Vector_ref<Rectangle> pt;
    for (int i = 0; i < POINTS; ++i)
    {
         pt_test;
     cout << "i == " << i << "  points == " << POINTS << endl;
         pt.push_back(new Rectangle(Point(x,y),5,5));
         pt[i].set_fill_color(Color::yellow);
         win.attach(pt[i]); 
    }
}

我还注意到,当body在循环中时,函数add_pts不起作用,但是如果你将body放在int_main()中,它会无限期地运行但是没有快速到达分段错误,如果一点都不。

0 个答案:

没有答案