//============================================================================
// 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.
Related
I have an assignment where i should make code that draw a vertical triangular wave But here is a problem the function which is drawOnePeriod that calls the function drawwave calls it with a constant number 5 , this number determines the amplitude for the wave however, if i want to change this number to modify the amplitude , it mess up the pattern so i was wondering if i can get some help anywhere cause i can't think of a relation.
# include <iostream>
using namespace std;
void drawOnePeriod(int periodLength)
{
int z = 10;
for (int i = 1; i < periodLength; i++)
{
for (int j = 0; j <= z; j++)
{
cout << " ";
}
z++;
cout << "*" << endl;
}
for (int i = 1; i <= 3 + periodLength; i++)
{
for (int j = 0; j <= z; j++)
{
cout << " ";
}
z--;
cout << "*" << endl;
}
for (int i = 1; i < periodLength; i++)
{
for (int j = 0; j <= z; j++)
{
cout << " ";
}
z++;
cout << "*" << endl;
}
}
void drawWave(int nPeriods)
{
for (int i = 1; i <= nPeriods; i++)
{
drawOnePeriod(5);
}
}
int main()
{
int n;
cin >> n;
drawWave(n);
}
It would really help if you included what output you expect.
Here is my interpretation of a "vertical triangular wave"
#include <stdio.h>
int main(void) {
int amplitude = 8;
int cycles = 3;
int delta = +1;
int i=1;
while (cycles --> 0)
{
for(; i>=1 && (i<=amplitude) ; i += delta)
{
printf("%*s\n", i, "*");
}
i += 2*(delta = -delta);
}
return 0;
}
IDEOne Link
Output
Success #stdin #stdout 0s 4448KB
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
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;
}
For some reason I can't get the distance function to work. Could someone explain to me what I am doing wrong?
Main Function:
#include <iostream>
#include <cmath>
using namespace std;
int distance(int**, int);
int main()
{
int m;
int goal [3][3] = { {1,2,3},{4,5,6},{7,8,0}};
const int N = 3;
int intial[N][N];
cout << "Enter in values: \n";
for(int i=0; i<3; i++) //This loops on the rows.
{
for(int j=0; j<3; j++) //This loops on the columns
{
cin >> intial[i][j];
}
}
cout << goal[0][0] << "\n" ;
m = distance(intial,N);
system("PAUSE");
return 0;
}
Manhattan Distance calculation!
int distance(int** array,int N)
{
int MSum = 0;
for (int x = 0; x < N; x++){ //Transversing rows(i)
for (int y = 0; y < N; y++) { //traversing colums (j)
int value = array[x][y]; // sets int to the value of space on board
if (value != 0) { // Don't compute MD for element 0
int targetX = (value - 1) / N; // expected x-coordinate (row)
int targetY = (value - 1) % N; // expected y-coordinate (col)
int dx = x - targetX; // x-distance to expected coordinate
int dy = y - targetY; // y-distance to expected coordinate
MSum += abs(dx) +abs(dy);
}
}
}
int m = MSum;
return m;
}
This looks basically correct as far as logic goes. The array you pass in should be compatible with the function declaration and implementation. Here is a sample declaration:
int distance(int array[3][3],int N);
this error keeps coming up yet i dont see a problem with the code (its in c++)
the program is supposed to find the inverse of a 2x2 matrix
#include <iostream>
using namespace std;
int main() {
float d;
float A[2][2], B[2][2];
do {
cout << "please enter valid parameters in for 11,12,21,22" << endl;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++)
cin >> A[i][j];
}
d = (A[1][1] * A[2][2]) - (A[1][2] * A[2][1]);
} while(d == 0);
B[1][1] = A[2][2] * (1.0 / d);
B[1][2] = A[1][2] * (-1.0 / d);
B[2][1] = A[2][1] * (-1.0 / d);
B[2][2] = A[1][1] * (1.0 / d);
for(int k = 0; k < 2; k++) {
for(int h = 0; h < 2; h++)
cout << B[k][h] << " ";
cout << endl;
}
return 0;
}
You are indexing B and A from 1 to 2 , instead use it from 0 to 1.
#include <iostream>
#include <math.h>
#include <cstdlib>
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 = 0;
int b = 0;
int c = 0;
int d = 0;
int x = 0;
int y = 0;
int radius = 0;
char choice;
cout << "Type 'c' to draw a circle or type 'l' to draw a line." << endl;
cin >> choice;
if (choice == 'c'){
cout << "please enter an x coordinate for the center of the circle \n";
cin >> x;
cout << "please enter a y coordinate for the center of the circle \n";
cin >> y;
cout << "please enter a value for the radius of the circle \n";
cin >> radius;
int moves = (x - radius) / 10;
for (int s = 0; s < moves; s++){
circle(x, y, radius);
system("clear");
x = x -10;
}
}
else if (choice == 'l'){
cout << "Please enter the x coordinate for the first point on the line \n";
cin >> a;
cout << "Please enter the y coordinate for the first point on the line \n";
cin >> b;
cout << "Please enter the x coordinate for the end point on the line \n";
cin >> c;
cout << "Please enter the y coordinate for the end point on the line \n";
cin >> d;
}
else
cout << "you did not enter an appropriate letter, please restart the program and try again."<< endl;
return 0;
}
void circle(int x, int y, int radius){
if (x + radius >= 81|| x - radius <= 0 || y + radius >= 26 || y - radius <= 0){
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;
}
}
for (int row = 25; row >= 0; row--) {
for (int col = 0; col < 81; col++) {
cout << drawSpace[row][col];
}
cout << "\n";
}
}
void line(int a, int b, int c, int d){
if (a >= 81 || c >= 81 || a <= 0 || c <= 0 || b >= 26 || d >= 26 || b <= 0 || d <= 0){
return;
}
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 = 21; j < 81; j++) {
if (slope > 0){
if (j > a && j < c){
int newIntercept = i - (slope*j);
int test = abs (intercept - newIntercept);
if (test <= 0)
buffer[i][j] = true;
else
buffer[i][j] = false;
}
}
else if (slope < 0){
if (j < a && j > c){
int newIntercept = i - (slope*j);
int test = abs (newIntercept - intercept);
if (test <= 0)
buffer[i][j] = true;
}
else
break;
}
}
}
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;
}
}
for (int row = 25; row >= 0; row--) {
for (int col = 0; col < 81; col++) {
cout << drawSpace[row][col];
}
cout << "\n";
}
}
I have written this code for a programming assignment, the goal of which is to take inputs for the coordinates and dimensions of a circle or line, and to print them out to the terminal as if it were a graph. The second step is to get the shape to move from the right side of the screen to the left. I have started to write this code for the circle, however for some reason the system("clear") call does not seem to clear the screen, and it simply prints extra circles without getting rid of the older one. If someone could help I would really appreciate it.
Try:
cout << "\033[2J\033[1;1H";
Go http://en.wikipedia.org/wiki/ANSI_escape_code for more information.
On Linux (and other Unixes) you could also use the ncurses library to output to a terminal.
The original poster doesn't have enough rep yet, so I'm posting this here for him:
I was actually a bit off base. The system("clear") I was using actually did
work, the problem I was encountering was that I did not reset the bool
array I was using to plot out the points that needed to be drawn.
Thanks for the help, I learned a few things about how to clear the screen before I found my own problem.