#include <string> #include <iostream> #include <fstream> #include <cmath> using
ID: 3886413 • Letter: #
Question
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
/
struct point {
int x, y;
// constructor
point(int _x = 0, int _y = 0)
: x(_x), y(_y) {}
};
// struct for rgb-pixel, holds red, green and blue values
struct rgb {
int r, g, b;
// constructor
rgb(int _r = 0, int _g = 0, int _b = 0)
: r(_r), g(_g), b(_b) {}
};
class PPMImage {
public:
PPMImage(int width, int height) : w(width), h(height) {
pixmap = new rgb*[h];
for (int i = 0; i < h; i++)
pixmap[i] = new rgb[w];
}
// destructor, clears data
~PPMImage() {
w = 0;
h = 0;
for (int i = 0; i < h; ++i){
delete[] pixmap[i];
delete[] pixmap;
}
}
void fill(rgb);
void draw_Polygon(point, point, rgb);
void draw_line(point, point, rgb);
bool print_to_file(const string&) const;
private:
// image width and height
int w, h;
// image pixels. I know you said not to use double pointers, but it works because when I use one pointer the program crashes or gives me errors.
rgb **pixmap;
};
void PPMImage::fill(rgb col) {
// loop and assign put col in each pixel
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
pixmap[i][j] = col;
}
}
}
void PPMImage::draw_Polygon(point c, point a, rgb col) {
int k, b;
int dx = abs(c.x - a.x);
int dy = abs(c.y - a.y);
// if the x length is longer
if (dx > dy)
{
// iterate through all x values
for (int x = std::min(c.x, a.x); x <= std::max(c.x, a.x); x++)
{
int y = (c.y - a.y)*(x - c.x)/(a.x - c.x) + c.y; // got from (y - y1)/(y2 - y1) = (x - x1)(x2 - x1)
pixmap[y][x] = col;
}
} else
{
for (int y = std::min(c.y, a.y); y <= std::max(c.x, a.x); y++)
{
int x = (a.x - c.x)*(y - c.y)/(a.y - c.y) + c.x; // got from (y - y1)/(y2 - y1) = (x - x1)(x2 - x1)
pixmap[y][x] = col;
}
}
}
void PPMImage::draw_line(point p1, point p2, rgb col) {
// swap points so p1.x if lesser than p2.x
if (p1.x > p2.x) {
point temp = p1;
p1 = p2;
p2 = temp;
}
// loop increment value
int dy = p1.y < p2.y ? 1 : -1,
// length of the each subline
len = (p2.x - p1.x + 1) / (abs(p1.y - p2.y) + 1);
// draw each subline
while (p1.y != p2.y) {
for (int i = 0; i < len; ++i) {
pixmap[p1.y][p1.x] = col;
}
p1.y += dy;
}
// fill remaining pixels
while (p1.x < p2.x) {
pixmap[p2.y][p1.x++] = col;
}
}
// prints image to file
bool PPMImage::print_to_file(const string &filename) const {
ofstream out(filename.c_str());
// couldn't open file
if (!out){
return false;
}
out << "P3" << endl;
out << w << " " << h << endl;
out << 255 << endl;
// fill each pixel
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
rgb &col = pixmap[i][j];
out << col.r << " " << col.g << " " << col.b << endl;
}
}
return true;
}
// constants
const int WIDTH = 320,
HEIGHT = 240;
point line1 = point(60, 120);
point line2 = point(110, 200);
point line3 = point(110, 150);
point line4 = point(200, 220);
point line5 = point(160, 120);
// colors for background and line
rgb LIGHT_GRAY = rgb(245, 245, 245),
Magenta = rgb(255, 0, 255);
int main() {
int gd = DETECT, gm;
initgraph (&gd, &gm, NULL);
// create image
PPMImage img(WIDTH, HEIGHT);
// fill background
img.fill(LIGHT_GRAY);
img.draw_Polygon(line1, line1, Magenta);
img.draw_Polygon(line2, line2, Magenta);
img.draw_Polygon(line3, line3, Magenta);
img.draw_Polygon(line4, line4, Magenta);
img.draw_Polygon(line5, line5, Magenta);
// print image
img.print_to_file("Image.ppm");
return 0;
}
**************************error at line 167 detect not declared in this scope 168 gm and intgraph not declared in this scope - trying to draw eliptical in magenta - c++ ppm file
Explanation / Answer
Hi,
It happens because graphis.h is not included in the program.
=====================================================================================
Try including below header file
#include<graphics.h>
Now after including and recompiling throws error like"No such file or directory".Then try to do the following activity:-
If you are using Dev C++ development environment then follow the below steps:-
1) Download graphics.h and save it in below path:-
include/subdirectory of DevC++
2)Download libbgi.a and save it in below path:-
lib/
3)PRESS ATL+P
a) choose parameters tab
b) Key in below words:-
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32
Re-compile the program .It should run.
========================================================================================
Please let me know in case any queries.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.