By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

displaying output from this program

P: n/a
My program is supposed to draw lines given from certain vertices that
are given to me. the colors from the lines are also defined. It should
display on a black screen. I am not sure how to do this at all. The
algorithm that I have created is bresenham's algorithm and the Color
class allows one to create different colors. Could someone show me how
to output or display my program. It would greatly be appreciated.

// -----------------------------------------------------
// Line.cpp
// -----------------------------------------------------

#include "line.h"

Line::Line(int xres, int yres): xres(xres), yres(yres)
{
image =new Color*[yres];
for (int i=0;i<yres;i++)
image[i] = new Color[xres];
gridCell.clear();
}
Line::~Line()
{
if(image)
{
for (int i=0;i<yres;i++)
delete[] image[i];

delete[] image;
}
}

void Line::save(char* filename)
{

}

void Line::draw(int x0, int y0, int x1, int y1, Color c)
{

int d, x, y, ax, ay, sx, sy, dx, dy;

dx = x1 - x0;
ax = abs(dx) << 1;
sx = sgn(dx);

dy = y1 - y0;
ay = abs(dy) << 1;
sy = sgn(dy);

x = x0;
y = y0;

if (ax > ay) {

d = ay - (ax >> 1);
while (x != x1) {
plot(x, y, c);
if (d >= 0) {
y += sy;
d -= ax;
}
x += sx;
d += ay;
}

} else {

d = ax - (ay >> 1);
while (y != y1) {
plot(x, y, c);
if (d >= 0) {
x += sx;
d -= ay;
}
y += sy;
d += ax;
}

}

plot(x1, y1, c);

}

void Line::plot(int x, int y, Color c)
{

GridCell gc;

gc.x = x;
gc.y = y;
gc.c = c;

gridCell.push_back(gc);

}

int main()
{
Color red=Color(1,0,0);
Color green=Color(0,1,0);
Color purple=Color(1,0,1);
Color blue=Color(0,0,1);
Color white=Color(0,0,0);
Color yellow=Color(1,1,0);
Color cyan=Color(0,1,1);
Color orange=Color(1,1/2,0);
return 0;
}

// -----------------------------------------------------
// Line.h
// -----------------------------------------------------

#ifndef __LINE_H__
#define __LINE_H__

#include <iostream>
#include <vector>
using namespace std;

/* A Color is a RGB float.
**
** R, G, and B are all in the range [0..1]
**
** This class allows you to add, subtract, and multiply colors,
** It also allows you to get the separate components (e.g.,
myColor.red() ),
** use constructions like "myColor += yourColor;"
*/
class Color
{
float r,g,b;
public:
inline Color(): r(0), g(0), b(0) {}
inline Color(float r, float g, float b) : r(r), g(g), b(b){}
inline ~Color() {}
inline Color operator*(const Color& c) const
{
return Color(r*c.r, g*c.g, b*c.b);
}
inline Color operator+(const Color& c) const
{
return Color(r+c.r, g+c.g, b+c.b);
}
inline Color operator-(const Color& c) const
{
return Color(r-c.r, g-c.g, b-c.b);
}
inline Color operator*(float s) const
{
return Color(r*s, g*s, b*s);
}
inline Color& operator+=(const Color& c)
{
r+=c.r;
g+=c.g;
b+=c.b;
return *this;
}
inline float red() const
{
return r;
}
inline float green() const
{
return g;
}
inline float blue() const
{
return b;
}
inline float luminance() const
{
return (float)(0.3*g + 0.6*r + 0.1*b);
}

inline float max_component() const
{
float temp = (g > r? g : r);
return (b > temp? b : temp);
}
};

typedef struct {

int x, y;
Color c;

} GridCell;

class Line
{

float* buf;
Color** image;
int xres, yres;

public:

Line(int xres, int yres);
~Line();
void draw(int, int, int, int, Color);
void save(char* file);
vector<GridCell> getGridCells(void);
inline int getXRes() const
{
return xres;
}
inline int getYRes() const
{
return yres;
}
inline Color& operator()(int x, int y)
{
return image[y][x];
}

private:

int abs(int);
int sgn(int);
void plot(int, int, Color);
vector<GridCell> gridCell;
};

inline int Line::abs(int a)
{

return (((a) < 0) ? -(a) : a);

}

inline int Line::sgn(int a)
{

return (((a) < 0) ? -1 : 1);

}

inline vector<GridCell> Line::getGridCells()
{

return gridCell;

}

#endif // __LINE_H__

Sep 5 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
George wrote:
My program is supposed to draw lines given from certain vertices that
are given to me. the colors from the lines are also defined. It should
display on a black screen. I am not sure how to do this at all.

You are not going to like this answer but C++ does not have any way of
doing any kind of graphics at all.

If you want to do graphics in C++ you have to use a library that has the
graphics code built into it. So the first thing you need to decide is
which graphics library you are going to use to display the lines you
want to display. This is not a C++ question at all, and you shouldn't be
asking about it here.

The factors that will influence which library you choose are things like
what operating system are you programming on, what kind of graphics to
you want to do, how much are you prepared to pay, etc. etc. None of
these have anything to do with C++.

john
Sep 5 '05 #2

P: n/a

"George" <bu*******@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
My program is supposed to draw lines given from certain vertices that
are given to me. the colors from the lines are also defined. It should
display on a black screen. I am not sure how to do this at all. The
algorithm that I have created is bresenham's algorithm and the Color
class allows one to create different colors. Could someone show me how
to output or display my program. It would greatly be appreciated.


It totally depends on your OS and what graphics you decide to go with.

You'll need to research for your OS what type of graphics libraries you have
and which one you want to use.
Sep 7 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.