How to implement an efficient im2col function in C++ using OpenCV? - c++

I've been trying to implement im2col function present in MATLAB and GNU Octave. I found it hard to understand the implementation present in Octave's source code, so I ran the function on few matrices to understand the logic behind it. Using that, I've implemented the same in C++ using OpenCV, and although the result seems to be the same, it's awfully slow.
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat input = Mat::eye(100,100,CV_32FC1);
input.at<float>(1,2) = 2; //Makes it easier to verify the correct solution
int rowBlock = 7;
int colBlock = 5;
int m = input.rows;
int n = input.cols;
int x = m - rowBlock + 1;
int y = n - colBlock + 1;
Mat result = Mat::zeros(1,rowBlock*colBlock,CV_32FC1);
for(int i = 0; i< y; i++)
{
for (int j = 0; j< x; j++)
{
Mat temp2 = input.rowRange(j,j+rowBlock).colRange(i,i+colBlock).t();
temp2 = temp2.reshape(1,1);
vconcat(result,temp2,result);
}
}
result = result.rowRange(1,result.rows);
cout << result << endl;
return 0;
}
Is there any way to improve on it? I'm sure I might be doing a lot of things very inefficiently here.

This one is much faster for me:
int main()
{
cv::Mat input = cv::Mat::eye(100,100,CV_32FC1);
input.at<float>(1,2) = 2; //Makes it easier to verify the correct solution
int rowBlock = 7;
int colBlock = 5;
int m = input.rows;
int n = input.cols;
// using right x = col; y = row
int yB = m - rowBlock + 1;
int xB = n - colBlock + 1;
// you know the size of the result in the beginning, so allocate it all at once
cv::Mat result2 = cv::Mat::zeros(xB*yB,rowBlock*colBlock,CV_32FC1);
for(int i = 0; i< yB; i++)
{
for (int j = 0; j< xB; j++)
{
// here yours is in different order than I first thought:
//int rowIdx = j + i*xB; // my intuition how to index the result
int rowIdx = i + j*yB;
for(unsigned int yy =0; yy < rowBlock; ++yy)
for(unsigned int xx=0; xx < colBlock; ++xx)
{
// here take care of the transpose in the original method
//int colIdx = xx + yy*colBlock; // this would be not transposed
int colIdx = xx*rowBlock + yy;
result2.at<float>(rowIdx,colIdx) = input.at<float>(i+yy, j+xx);
}
}
}
// check your output here...
}
I added it to your code, to test the equality (it would be better to write a function for each one and encapsulate, though ;) )
int main()
{
cv::Mat input = cv::Mat::eye(100,100,CV_32FC1);
input.at<float>(1,2) = 2; //Makes it easier to verify the correct solution
int rowBlock = 7;
int colBlock = 5;
int m = input.rows;
int n = input.cols;
// here, your naming of x and y is counter intuitive for me, since I see x being linked to cols normally (e.g. direction of x-axis)
int x = m - rowBlock + 1;
int y = n - colBlock + 1;
cv::Mat result = cv::Mat::zeros(1,rowBlock*colBlock,CV_32FC1);
for(int i = 0; i< y; i++)
{
for (int j = 0; j< x; j++)
{
cv::Mat temp2 = input.rowRange(j,j+rowBlock).colRange(i,i+colBlock).t();
temp2 = temp2.reshape(1,1);
cv::vconcat(result,temp2,result);
}
}
result = result.rowRange(1,result.rows);
std::cout << result.rows << " x " << result.cols << std::endl;
char w;
std::cin >> w;
// using right x = col; y = row
int yB = m - rowBlock + 1;
int xB = n - colBlock + 1;
// you know the size of the result in the beginning, so allocate it all at once
cv::Mat result2 = cv::Mat::zeros(x*y,rowBlock*colBlock,CV_32FC1);
for(int i = 0; i< yB; i++)
{
for (int j = 0; j< xB; j++)
{
// here yours is in different order than I first thought:
//int rowIdx = j + i*xB; // my intuition how to index the result
int rowIdx = i + j*yB;
for(unsigned int yy =0; yy < rowBlock; ++yy)
for(unsigned int xx=0; xx < colBlock; ++xx)
{
// here take care of the transpose in the original method
//int colIdx = xx + yy*colBlock; // this would be not transposed
int colIdx = xx*rowBlock + yy;
result2.at<float>(rowIdx,colIdx) = input.at<float>(i+yy, j+xx);
}
}
}
std::cout << result2.rows << " x " << result2.cols << std::endl;
std::cin >> w;
// test whether both results are the same:
bool allGood = true;
for(int j=0; j<result.rows; ++j)
for(int i=0; i<result.cols; ++i)
{
if(result.at<float>(j,i) != result2.at<float>(j,i))
{
std::cout << "("<<j<<","<<i<<") = " << result.at<float>(j,i) << " != " << result2.at<float>(j,i) << std::endl;
allGood = false;
}
}
if(allGood) std::cout << "matrices are equal" << std::endl;
std::cin >> w;
return 0;
}

Related

Write a C++ program that finds and lists all possible correct credit card numbers according to the Luhn algorithm

I need to print all possible correct credit card numbers according to the Luhn algorithm, and I did like a 16 for loops nested inside each other,
I was wondering if there is a way to make my code shorter?
Here is my code:
this is for index 0,2,4,6,8,10,12,14,16 It takes the number and multiply it by 2. and after that it takes the sum of the individual numbers; lets say index 0 is 6 it takes 6*2=12 and sums 1+ 2
int dbl(int x) {
int sum = 0;
while (x !=0)
{
sum += x % 10;
x /= 10;
}
return sum;
}
void bruh(int x, int y) {
x += (dbl(2 * y));
}
This is my loops:
int main()
{
for (int i = 0; i <= 9; i++)
{
for (int j = 0; j <= 9; j++)
{
for (int k = 0; k <= 9; k++)
{
for (int l = 0; l <= 9; l++)
{
for (int m = 0; m <= 9; m++)
{
for (int n = 0; n <= 9; n++)
{
for (int o = 0; o <= 9; o++)
{
for (int p = 0; p <= 9; p++)
{
for (int q = 0; q <= 9; q++)
{
for (int r = 0; r <=9; r++)
{
for (int s = 0; s <= 9; s++)
{
for (int u = 0; u <= 9; u++)
{
for (int v = 0; v <= 9; v++)
{
for (int x = 0; x <= 9; x++)
{
for (int w = 0; w <= 9; w++)
{
for (int y = 0; y <= 9; y++)
{
int dbles = 0, sngls =0;
bruh(dbles, i);
sngls += j;
bruh(dbles, k);
sngls += l;
bruh(dbles, m);
sngls += n;
bruh(dbles, o);
sngls += p;
bruh(dbles, q);
sngls += r;
bruh(dbles, s);
sngls += u;
bruh(dbles, v);
sngls += x;
bruh(dbles, w);
sngls += y;
if (dbles+sngls%10==0)
{
cout << "Valid Number: " << i << j << k << l << m << n << o << p << q << r << s << u << v << x << w << y << endl;
cout << "---------------" << endl;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return 0;
}
The program works fine(added a break statement to stop after 100th cc to check if it runs, And it dose), I just want to know if there is away to make my code shorter
Something like this will be much shorter and easier to read:
#include <string>
void increment(std::string& s) {
for (int i = s.length() - 1; i >= 0; --i) {
if (s[i] != '9') {
s[i]++;
return;
}
s[i] = '0';
}
}
int main()
{
std::string s = "0000000000000000";
for (uint64_t i = 0; i < 1'000'000'000'000'000; ++i) {
// do your check with s[0]..s[15]
increment(s);
}
}
NOTE: this does pretty much what you tried to do in your code.
You can take n0rd's suggestion and loop through 15 digit numbers, calculating the checksum and appending it to the end. In that case you'd only need 3,000 years instead of 30,000 :)

Merge sort errors c++

I'm very new to C++ and only coded in python before, but python is too slow for my purposes now. I did a mergesort algorithm in python and it worked. But now I translated it into C++ and I got a bunch of errors in my IDE. What are my errors?
#include <iostream>
using namespace std;
int *sort(int lenght, int lis[]) {
int units = lenght;
int umt;
int tiles = 1;
while (units > 1) {
bool whole = true;
umt = units % 2;
if (umt = 1) {
units++;
whole = false;
}
units = units / 2;
tiles = tiles * 2;
if (whole) {
int buffd[units];
int add_l = 0;
int add_r = 0;
int prod_l = 0;
int prod_r = prod_l + tiles / 2;
for (int k = 0; k < units; k++) {
int buffd[units];
int add_l = 0;
int add_r = 0;
int prod_l = k * tiles;
int prod_r = prod_l + tiles / 2;
for (int f = 0; f < tiles; f++) {
if (lis[prod_l + add_l] <= lis[prod_r + add_r]) {
buffd[f] = lis[prod_l + add_l];
add_l++;
if (add_l = tiles / 2) {
for (int e = f; e < tiles; e++) {
buffd[e] = lis[prod_r + add_r + e];
}
f = tiles;
}
} else {
buffd[f] = lis[prod_r + add_r];
add_r++;
if (add_r = tiles / 2) {
for (int e = f; e < tiles; e++) {
buffd[e] = lis[prod_l + add_l + e];
}
f = tiles;
}
}
}
for (int i = prod_l; i < prod_l + tiles; i++) {
lis[i] = buffd[i - prod_l];
}
}
} else {
int buffd[units];
int add_l = 0;
int add_r = 0;
int prod_l = 0;
int prod_r = prod_l + tiles / 2;
for (int k = 0; k < units - 1; k++) {
int buffd[units];
int add_l = 0;
int add_r = 0;
int prod_l = k * tiles;
int prod_r = prod_l + tiles / 2;
for (int f = 0; f < tiles; f++) {
if (lis[prod_l + add_l] <= lis[prod_r + add_r]) {
buffd[f] = lis[prod_l + add_l];
add_l++;
if (add_l = tiles / 2) {
for (int e = f; e < tiles; e++) {
buffd[e] = lis[prod_r + add_r + e];
}
f = tiles;
}
} else {
buffd[f] = lis[prod_r + add_r];
add_r++;
if (add_r = tiles / 2) {
for (int e = f; e < tiles; e++) {
buffd[e] = lis[prod_l + add_l + e];
}
f = tiles;
}
}
}
}
for (int i = prod_l; i < prod_l + tiles; i++) {
lis[i] = buffd[i - prod_l];
}
}
}
return lis;
}
int main() {
int to_sort[8] = { 23, 1, 654, 2, 4, 87, 3, 1 };
cout << "sortiert: ";
int *sorted;
sorted = sort(8, to_sort);
for (int p = 0; p < 8; p++) {
cout << sorted[p] << " ";
}
return 0;
}
The errors are in German and I have no idea why, the rest of the IDE is in English. Does anyone know how to set that to English, I'm using Clion from JetBrains.
There are some major problems in your code:
comparisons must use == instead of =, which is the assignment operator.
the redundant definitions for buffd, add_l, add_r, prod_l and prod_r should me removed.
variable length array definitions such as int buffd[units] are not supported by many C++ compilers. These are extensions for compatibility with C90 optional features, likely to cause stack overflow for large arrays. You should allocate these arrays or use std::vector.
these local arrays are declared with a incorrect size: it should be int buffd[tiles];, not int buffd[units]. Undefined behavior ensues.
the last for loop is outside the body of the previous loop, which is incorrect.
you do not increment f before copying the remaining elements from the other slice when either add_l or add_r equals tiles / 2.
your non-recursive algorithm cannot succeed in the general case, I got it to work for array lengths that are powers of 2, and it is quite surprising that it may come as a translation from your python version. There are much simpler ways to program mergesort in python, and in C++ too.
With some extra work, I simplified your code and got it to work for the general case:
#include <iostream>
using namespace std;
int *sort(int length, int lis[]) {
for (int tile = 1; tile < length; tile += tile) {
int tiles = tile + tile;
int *buffd = new int[tiles];
for (int prod_l = 0; prod_l < length; prod_l += tiles) {
int add_l = 0;
int max_l = tile;
int add_r = 0;
int max_r = tile;
int prod_r = prod_l + max_l;
int f = 0;
if (prod_r >= length)
break;
if (prod_r + max_r > length)
max_r = length - prod_r;
for (;;) {
if (lis[prod_l + add_l] <= lis[prod_r + add_r]) {
buffd[f++] = lis[prod_l + add_l++];
if (add_l == max_l) {
while (add_r < max_r) {
buffd[f++] = lis[prod_r + add_r++];
}
break;
}
} else {
buffd[f++] = lis[prod_r + add_r++];
if (add_r == max_r) {
while (add_l < max_l) {
buffd[f++] = lis[prod_l + add_l++];
}
break;
}
}
}
for (int i = 0; i < f; i++) {
lis[prod_l + i] = buffd[i];
}
}
delete[] buffd;
}
return lis;
}
int main() {
int to_sort[8] = { 23, 1, 654, 2, 4, 87, 3, 1 };
for (int i = 1; i < 8; i++) {
cout << "sortiert: ";
int *sorted = sort(i, to_sort);
for (int p = 0; p < i; p++) {
cout << sorted[p] << " ";
}
cout << endl;
}
return 0;
}
Here is a classic top-down recursive implementation for reference:
void mergesort(int lis[], int lo, int hi, int *tmp) {
if (hi - lo >= 2) {
int mid = (hi - lo) / 2;
mergesort(lis, lo, lo + mid, tmp);
mergesort(lis, lo + mid, hi, tmp);
for (int i = 0; i < mid; i++)
tmp[i] = lis[lo + i];
for (int i = 0, j = lo + mid, k = lo; i < mid;) {
if (j >= hi || tmp[i] <= lis[j])
lis[k++] = tmp[i++];
else
lis[k++] = lis[j++];
}
}
}
int *mergesort(int length, int lis[]) {
int *tmp = new int[length / 2];
mergesort(lis, 0, length, tmp);
delete[] tmp;
return lis;
}

Don't know what is wrong with my gaussian filter

I'm trying to implement a program to detect borders of an image and the first step I'm taking is applying a gaussian filter to remove noise. The problem is that the filter that I'm applying only makes the image darker and doesn't blur it as it is supposed to.
This is the code that I haVe at the moment:
void gaussFilter(C_Image &imagen, float desviation) {
float kernel[5][5];
float sumaKernel = 0;
float s = desviation * desviation;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
float xaxis = (1 / sqrt(2 * M_PI * s)) * pow(exp(-pow(i, 2)) / (2 * s), 1);
float yaxis = (1 / sqrt(2 * M_PI * s)) * pow(exp(-pow(j, 2)) / (2 * s), 1);
float gaussianFilter = xaxis * yaxis;
kernel[i][j] = gaussianFilter;
sumaKernel += kernel[i][j];
}
}
cout << "Suma kernel: " << sumaKernel << endl;
//Normaizamos el kernel;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
kernel[i][j] /= sumaKernel;
}
}
C_Image::IndexT rowN, colN, colorsN;
C_Image::BMPFileInfo("barbara_gray.bmp", rowN, colN, colorsN);
//filterCreation(kernel, 10);
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 5; y++)
{
if (y != 4) {
printf("%f|", kernel[x][y]);
}
else
{
printf("%f|\n", kernel[x][y]);
}
}
}
cout << "Numero de columnas: " << colN << "\nNumero de filas: " << rowN << "\nColores de la paleta: " << colorsN << endl;
float suma;
for (int x = 1; x <= imagen.LastRow(); x++)
{
for (int y = 1; y <= imagen.LastCol(); y++)
{
suma = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
suma += imagen(x, y) * kernel[i][j];
}
}
imagen(x, y) = suma;
}
}
}
And this is my main:
int main(int argc, char** argv)
{
char const* path = "barbara_gray.bmp";
C_Image imagen;
imagen.ReadBMP(path);
gaussFilter(imagen, 10);
char const* destinationPath = "result_image.bmp";
imagen.WriteBMP(destinationPath);
}
C_Image is a class given.

C++ replacing matrix values with sub-matrix values

I have a University assignment whereby I have a 1D array, containing 262144 values. I've created a matrix class which places these values into an object with the datasource being the double* list of 262144 values.
I need to be able to obtain a sub-matrix (which I'm able to do) from ANOTHER set of 262144 values (which I've also placed into a matrix object).
However, I'm having serious trouble and I've been trying so hard for the last 3 days to try and replace original matrix values from a sub-matrix. I've tried passing by reference, creating Matrix*'s. I've tried everything we've been taught and even researched a few more methods, all of which I haven't understood. I'll throw my code in here to see if anyone can explain a method to me which will be able to do this.
Matrix::Matrix()
{
"Matrix::Matrix() is invoked";
}
Matrix::Matrix(const Matrix& m)
{
"Matrix::Matrix(const Matrix&) is invoked";
_M = m._M;
_N = m._N;
_data = new double[_M*_N];
for (int i = 0; i < _M*_N; i++)
{
_data[i] = m._data[i];
}
}
Matrix::Matrix(int sizeR, int sizeC, double *input_data)
{
"Matrix::Matrix(int sizeR, int sizeC, double *input_data is invoked";
_M = sizeR;
_N = sizeC;
_data = new double[_M*_N];
for (int i = 0; i < _M*_N; i++)
{
_data[i] = input_data[i];
}
}
Matrix Matrix::get_Block(int start_row, int end_row, int start_coloumn, int end_coloumn)
{
int rows = (end_row - start_row);
int columns = (end_coloumn - start_coloumn);
int ctr = 0;
double *temp_Data = new double[rows*columns];
for (int x = start_row; x < (rows + start_row); x++)
{
for (int y = start_coloumn; y < (columns + start_coloumn); y++)
{
temp_Data[ctr] = get(x, y);
ctr++;
}
}
Matrix block(rows, columns, temp_Data);
delete[] temp_Data;
return block;
}
Matrix Matrix::operator+(const Matrix & other)
{
Matrix temp;
temp._M = other._M;
temp._N = other._N;
temp._data = new double[temp._M*temp._N];
for (int x = 0; x < (temp._M*temp._N); x++)
{
temp._data[x] = this->_data[x] + other._data[x];
}
return temp;
}
Matrix Matrix::operator*(const Matrix & other)
{
Matrix temp;
temp._M = other._M;
temp._N = other._N;
temp._data = new double[temp._M*temp._N];
for (int x = 0; x < (temp._M*temp._N); x++)
{
temp._data[x] = this->_data[x] * other._data[x];
}
return temp;
}
Matrix Matrix::operator-(const Matrix & other)
{
Matrix temp;
temp._M = other._M;
temp._N = other._N;
temp._data = new double[temp._M*temp._N];
for (int x = 0; x < (temp._M*temp._N); x++)
{
temp._data[x] = this->_data[x] - other._data[x];
}
return temp;
}
void Matrix::replace_Block(Matrix& noisy, Matrix& shuffled,int k, int j, int i)
{
int val_to_replace = 0;
for (int i = 0; i < 3 * 3; i++)
{
val_to_replace = shuffled.get(i, j);
noisy.set(i, j, val_to_replace);
}
}
void Matrix::set_Block(Matrix block, Matrix& Noisy, int start_row, int end_row)
{
int ctr = 0;
int ctr2 = 0;
int ctr3 = 0;
for (int i = 0; i < 3; i++)
{
Noisy._data[(start_row*_M)+i+4] = block.get(i, ctr);
ctr++;
}
for (int j = 0; j < 3; j++)
{
Noisy._data[((start_row + 1)*_M) + j + 3] = block.get(j, ctr2);
ctr2++;
}
for (int j = 0; j < 3; j++)
{
Noisy._data[((start_row + 1)*_M) + j + 2] = block.get(j, ctr3);
ctr3++;
}
}
double Matrix::get_Sum(Matrix m)
{
double total = 0;
short row = m.get_M();
short column = m.get_N();
for (int j = 0; j < row; j++)
{
for (int i = 0; i < column; i++)
{
total += m.get(j,i);
}
}
return total;
}
double Matrix::get_Sum(Matrix* m)
{
double total = 0;
short row = m->get_M();
short column = m->get_N();
for (int j = 0; j < row; j++)
{
for (int i = 0; i < column; i++)
{
total += m->get(i, j);
}
}
return total;
}
double Matrix::get(int i, int j)
{
return _data[(i * _M) + j];
}
void Matrix::write_Block(int i, int j)
{
for (int ctr = 0; ctr < i; ctr++)
{
for (int ctr2 = 0; ctr2 < j; ctr2++)
{
std::cout << " " << this->get(ctr,ctr2);
}
std::cout << std::endl;
}
}
void Matrix::set(int i, int j, double val)
{
this->_data[(i*_M) + j] = val;
}
void Matrix::set_N(int N)
{
_N = N;
}
void Matrix::set_M(int M)
{
_M = M;
}
int Matrix::get_N()
{
return _N;
}
int Matrix::get_M()
{
return _M;
}
Matrix::~Matrix()
{
"Matrix::~Matrix() is invoked";
delete[] _data;
}
If it would be helpful to see main() I can supply that too, however all it really contains is the creation of the matrix objects using overloaded constructors.
explanation
Answer is only 4 years late . . .
Anyway. Maybe it will help somebody else. The secret is to use a std::valarray. With that it is utmost simple to work on a matrix. And, many many functions are available.
All the functions that you want to implement are already available.
And you sub-matrix coy can be a one liner . . .
Please see example code:
#include <iostream>
#include <algorithm>
#include <numeric>
#include <valarray>
#include <iomanip>
constexpr size_t NRows = 6;
constexpr size_t NCols = 8;
constexpr size_t SubNRows = 2;
constexpr size_t SubNCols = 3;
void debugPrint(std::valarray<int> &v, size_t nrows = NRows, size_t ncols = NCols)
{
for (int r = 0; r < nrows; ++r) {
for (int c = 0; c < ncols; ++c)
std::cout << std::setw(3) << v[r*ncols+c] << ' ';
std::cout << '\n';
}
std::cout << '\n';
}
int main()
{
std::valarray<int> v1(NRows * NCols); // Define array with given size
std::iota(std::begin(v1),std::end(v1),0); // Fill the array with consecutive nunbers
debugPrint (v1); // Print the result
std::cout << "\nSum = " << v1.sum() << "\n\n"; // Print the sum of all values in matrix
std::valarray<int> v2(v1); // Create a 2nd matrix as a copy to the first
v2 += 100; // Add 100 to each value in the matrix
debugPrint(v2);
std::valarray<int> v3(NCols); // Get one column
v3 = v1[std::slice(2,NRows,NCols)];
debugPrint(v3,NRows,1);
std::valarray<int> subV2(SubNRows*SubNCols); // So, now the sub array
subV2 = v2[std::gslice(12,{SubNRows, SubNCols},{NCols,1})]; // Slice it out
debugPrint(subV2, SubNRows, SubNCols);
v1[std::gslice(25,{SubNRows, SubNCols},{NCols,1})] = subV2; // And copy to the first array
debugPrint (v1);
return 0;
}

Why is the axis for my program off

//============================================================================
// Name : Assignment.cpp
// Author : Tim Bialecki
// Version :
//============================================================================
#include <iostream>
#include <math.h>
using namespace std;
void circle(int x, int y, int radius);
void line(int a, int b, int c, int d);
bool buffer[26][81];
char drawSpace[26][81];
int main() {
int a = 75;
int b = 5;
int c = 4;
int d = 26;
/*cout << "please enter an x coordinate for the center of the circle";
cin >> x;
cout << "please enter a y coordinate for the center of the circle";
cin >> y;
cout << "please enter a value for the radius of the circle";
cin >> radius;*/
circle(a, b, c);
for (int col = 80; col >= 0; col--) {
for (int row = 25; row >= 0; row--) {
cout << drawSpace[row][col];
}
cout << "\n";
}
return 0;
}
void circle(int x, int y, int radius){
/*if (x + radius >= 81 || y + radius >= 26 || y - radius <= 26){
cout << "the coordinates provided for the circle will not fit on the screen" << endl;
return;
}*/
for (int i = 0; i < 26; i++) {
for(int j = 0; j < 81; j++) {
int a = abs (x - j);
int b = abs (y - i);
int distance = pow(a, 2) + pow(b, 2);
int realDistance = pow(radius, 2);
if (abs(realDistance - distance) <= 3){
buffer[i][j] = true;
}
}
}
for (int m = 0; m < 26; m++){
for(int n = 0; n < 81; n++){
if (buffer[m][n]){
drawSpace[m][n] = 42;
}
else
drawSpace[m][n] = 32;
}
}
}
void line(int a, int b, int c, int d){
int intercept = 0;
double rise = d - b;
double run = c - a;
double slope = rise/run;
intercept = b - (slope*a);
for (int i = 0; i < 26; i++) {
for(int j = 0; j < 81; j++) {
int newIntercept = i - (slope*j);
int test = abs (intercept - newIntercept);
if (test <= 0)
buffer[i][j] = true;
else
buffer[i][j] = false;
}
}
for (int m = 0; m < 26; m++){
for(int n = 0; n < 81; n++){
if (buffer[m][n])
drawSpace[m][n] = 42;
else
drawSpace[m][n] = 32;
}
}
}
This code is a work in progress, but I'm trying to write a program that takes inputs for the coordinates and dimensions of both a line and a circle and prints them out in the terminal window as if it were a 81x26 graph. I have just supplied sample inputs to test this out, but for some reason the shapes are not printing with the appropriate orientation to what should be the x and y axises. I have tried a bunch of different ways of trying to fix this problem and have had no luck. Hoping someone can help. Thanks
Looks OK to me:
***
** **
* *
* *
* *
* *
* *
** **
***
It's not perfectly round because characters are taller than they are wide.
EDIT: That's only the first few rows on my output. Based on the comment and a second look at the code, I think rows and columns are getting mixed up.
for (int col = 80; col >= 0; col--) {
for (int row = 25; row >= 0; row--) {
cout << drawSpace[row][col];
}
cout << "\n";
}
There's a newline after every "column". Swapping the two for lines may produce what you want.