G
George
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 = new Color[xres];
gridCell.clear();
}
Line::~Line()
{
if(image)
{
for (int i=0;i<yres;i++)
delete[] image;
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:lot(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__
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 = new Color[xres];
gridCell.clear();
}
Line::~Line()
{
if(image)
{
for (int i=0;i<yres;i++)
delete[] image;
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:lot(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__