Answer the question
In order to leave comments, you need to log in
How to properly parse a .pgm file in C++?
There is a task - to create an empty file with a .pgm image (just a white background) and draw a black line with arbitrary coordinates. It must be a P5 file containing bits, not just numbers. The task was implemented using the P2 format. How to handle file with P5 extension? And is it possible to use streams in the same way? For some reason, the teacher strongly suggests using fprintf. Here is the code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
void drawLine(int** array, int x1, int y1, int x2, int y2) {
const int deltaX = abs(x2 - x1);
const int deltaY = abs(y2 - y1);
const int signX = x1 < x2 ? 1 : -1;
const int signY = y1 < y2 ? 1 : -1;
//
int error = deltaX - deltaY;
//
array[x2][y2] = 0;
while (x1 != x2 || y1 != y2)
{
array[x1][y1] = 0;
const int error2 = error * 2;
//
if (error2 > -deltaY)
{
error -= deltaY;
x1 += signX;
}
if (error2 < deltaX)
{
error += deltaX;
y1 += signY;
}
}
}
int main(void)
{
ofstream fout;
fout.open("E:\\line.pgm");
fout.write("", 0);
fout << "P2" << "\n" ;
int numcols, numrows, x1, x2, y1, y2;
cout << "Enter rows and columns number: ";
cin >> numrows >> numcols;
cout << "Enter coords x1 and y1: ";
cin >> x1 >> y1;
cout << "Enter coords x2 and y2: ";
cin >> x2 >> y2;
fout << numrows << " " << numcols << "\n";
fout << 255 << "\n";
int** array = new int*[numrows];
for (int count = 0; count < numrows; count++)
array[count] = new int[numcols];
for (int i = 0; i < numrows; ++i) {
for (int j = 0; j < numcols; ++j) {
array[i][j] = 255;
}
}
drawLine(array, x1, y1, x2, y2);
for (int i = 0; i < numrows; ++i) {
for (int j = 0; j < numcols; ++j) {
fout << array[i][j] << " ";
}
}
fout.close();
}
Answer the question
In order to leave comments, you need to log in
Look towards Moodle. It's made for learning, but I'm not sure if it covers all your needs.
SEED( Source of education empowerment and development) is introduced an innovative unique software for an educational institutions to manage their institutions and it vanish the gap of communication between management and staff, staff to student/parents, "SEED "provides real time information through the parent portal.
If you are looking for the best School ERP Software than I will Suggest you.
Web-School
E-school ERP
School ERP
Fedena
If you are looking for the best School Management Software then I will Suggest you.
Schoollog School Management Software
Share great information about your blog , Blog really helpful for us . We read your blog , share the most useful information in blog . Thanks for sharing your blog here. With the aid of 60+ excellent module, School Management Software covers every aspect that you need to effectively manage all of the operations of the School.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question