开发者

Unable to push_back elements in a list

开发者 https://www.devze.com 2023-04-12 08:05 出处:网络
I\'m facing a really peculiar problem with my code, I am unable to push elements into a list. I\'m trying to implement a scan fill algorithm.

I'm facing a really peculiar problem with my code, I am unable to push elements into a list.

I'm trying to implement a scan fill algorithm. I want the list of points that I have plotted on the screen so that I can check if a scan line intersects with them. In my Screen::plot_pixel function I push the Point into the plotted_points list. But when I iterate through the list, it's empty.(I'm iterating in shape.cpp using a friend function)

I tried using a set too but to no avail. I have attached the console output I get too.

plot_pixel gets called multiple times, I verified this by adding a print statement there but the points refuse to get pushed in. Here's all my code, point.h

#ifndef POINT_H
#define POINT_H
class Point
{
    float x_coordinate,y_coordinate;
public:
    Point(){}
    Point(float x, float y):x_coordinate(x),y_coordinate(y){}
    float get_x() const{return x_coordinate;}
    float get_y() const {return y_coordinate;}

    bool operator==(const Point rhs)const
    {
        if( ((int)x_coordinate == (int)rhs.get_x()) && ((int)y_coordinate == (int)rhs.get_y()) )
            return true;
 开发者_如何学C       else return false;
    }

    bool operator<(const Point rhs)const
    {
        if((int)x_coordinate < (int)rhs.get_x())
            return true;
        else return false;
    }
};
#endif

screen.h

#ifndef SCREEN_H
#define SCREEN_H
#include<graphics.h>
#include "point.h"
#include<list>

class Shape;

class Screen
{
private:
    int max_x,max_y,grid_size;
    int delta_x,delta_y;
    int origin_x,origin_y;
public:
    std::list<Point> plotted_points;

    // Default Constructor to Initialize the screen with the grid
    Screen(int, int, int);

    //Method prototypes
    void plot_pixel(int,int,int);
    void dda_line(Point p1, Point p2);
    int get_max_x(){return max_x;}
    int get_max_y(){ return max_y;}
    friend void draw_shape(Screen,Shape);
    friend void fill_shape(Screen,Shape);    
};
#endif

screen.cpp

#include "screen.h"

void Screen::plot_pixel(int xcoord,int ycoord,int col=WHITE)
{
    int l,t,r,b;
    l = origin_x + xcoord * delta_x;
    r = l + delta_x;
    b = origin_y - ycoord * delta_y;
    t = b - delta_y;

    setcolor(col);
    bar(l,t,r,b);
    setcolor(WHITE);
    Point *tmp = new Point(xcoord,ycoord);

    //the culprit code
    plotted_points.push_back(*tmp);
    delete tmp;
}

void Screen::dda_line(Point p1, Point p2)
{
.....
    plot_pixel(x,y,0);
}

shape.h

#ifndef SHAPE_H
#define SHAPE_H

#include<list>
#include<iostream>
#include "point.h;

class Screen;

class Shape
{
    list<Point> figure;
public:
    Shape(list<Point> s);
    friend void draw_shape(Screen,Shape);
    friend void fill_shape(Screen,Shape);
};

#endif

shape.cpp

#include "shape.h"
#include "screen.h"

Shape::Shape(list<Point> s):figure(s){}
void fill_shape(Screen scr, Shape sh)
{
    list<Point>::iterator pit,vit;

    //HERE's Where I try iterating over the list and get nothing
    //to check if the plotted points and vertices are listed correctly
    cout<<"Plotted Points :\n";

    for( pit = scr.plotted_points.begin();pit != scr.plotted_points.end();pit++)
    {
        cout<<pit->get_x()<<" "<<pit->get_y()<<endl;
    }

    cout<<"Vertices :\n";

    for( vit = sh.figure.begin();vit != sh.figure.end();vit++)
    {
        cout<<vit->get_x()<<" "<<vit->get_y()<<endl;
    }
}

and finally my driver

#include "screen.h"
#include "shape.h"

using namespace std;

int main()
{
    Screen s(641,641,32);
    Point p1(-10,-10),p2(-10,10),p3(10,10),p4(10,-10);
    list<Point> rectangle_points;

    //to construct the rectangle
    rectangle_points.push_back(p1);
    rectangle_points.push_back(p2);
    rectangle_points.push_back(p3);
    rectangle_points.push_back(p4);
    Shape rectangle(rectangle_points);

    draw_shape(s,rectangle);
    fill_shape(s,rectangle);

    getch();

    return 0;
}

Here's the output

Plotted Points :
Vertices :
-10 -10
-10 10
10 10
10 -10


You are passing the Screen object s into your functions, but they take the Screen object by value. They therefore take a copy of the object which they then modify. Your original Screen "s" is unchanged after the call to draw_shape().

You should probably modify them so they take a reference to a Screen object, such as:

void draw_shape(Screen& scr, Shape shp)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号