Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Copyright © 2017 Bartosz Gronkiewicz. All rights reserved.
- //
- #include <iostream>
- #include <cmath>
- using namespace std;
- int main(int argc, char * argv[]) {
- const int sizeX = 50;
- const int sizeY = 20;
- int x1, x2, y1, y2, xb, xt, yb, yt;
- int dx, dy;
- char tab[sizeY][sizeX];
- if (argc == 5){
- x1 = atoi(argv[1]);
- y1 = atoi(argv[2]);
- x2 = atoi(argv[3]);
- y2 = atoi(argv[4]);
- if(y1 < y2) {
- xb = x1;
- yb = y1;
- xt = x2;
- yt = y2;
- }
- else {
- xb = x2;
- yb = y2;
- xt = x1;
- yt = y1;
- }
- dx = xt - xb;
- dy = yt - yb;
- //rysowanie układu współrzędnych
- for (int i = 0; i < sizeY; i++){
- for (int j = 0; j < sizeX; j++){
- if (i == sizeY/2){
- tab[i][j] = '-';
- }
- else if (j == sizeX/2){
- tab[i][j] = '|';
- }
- else{
- tab[i][j] = ' ';
- }
- if (i == sizeY/2 && j == sizeX/2){
- tab[i][j] = 'o';
- }
- }
- }
- //algorytm punktu środkowego
- if(abs(dx) > dy && dx > 0) {
- cout << "Pierwszy przypadek" << endl;
- double D = 2.0*(double)dy - (double)dx;
- int x = xb;
- int y = yb;
- while (x <= xt) {
- if(D > 0) {
- D = (double)D + 2.0*(double)dy;
- }
- else {
- D = 2.0*((double)dy-(double)dx);
- y++;
- }
- cout << "(" << x << ", " << y << ")" << endl;
- tab[sizeY/2 - y][sizeX/2 + x] = 'x';
- x++;
- }
- }
- //algorytm naiwny
- else if(abs(dx) < dy && dx > 0) {
- cout << "Drugi przypadek" << endl;
- int y = yb;
- int x = 0;
- while (y <= yt) {
- x = 0;
- x = (int)round((double)((double)dx/(double)dy) * (y - yb) + xb);
- cout << "(" << x << ", " << y << ")" << endl;
- tab[sizeY/2 - y][sizeX/2 + x] = 'x';
- y++;
- }
- }
- //algorytm naiwny
- else if(abs(dx) < dy && dx < 0) {
- cout << "Trzeci przypadek" << endl;
- int y = yb;
- int x = 0;
- while (y <= yt) {
- x = 0;
- x = (int)round((double)((double)dx/(double)dy) * (y - yb) + xb);
- cout << "(" << x << ", " << y << ")" << endl;
- tab[sizeY/2 - y][sizeX/2 + x] = 'x';
- y++;
- }
- }
- //algorytm naiwny
- else if(abs(dx) > dy && dx < 0) {
- cout << "Czwarty przypadek" << endl;
- int x = xb;
- int y = 0;
- while (x >= xt) {
- y = 0;
- y = (int)round((double)((double)dy/(double)dx) * (x - xb) + yb);
- cout << "(" << x << ", " << y << ")" << endl;
- tab[sizeY/2 - y][sizeX/2 + x] = 'x';
- x--;
- }
- }
- for (int i = 0; i < 20; i++){
- for (int j = 0; j < 50; j++){
- cout << tab[i][j];
- }
- cout << '\n';
- }
- }
- cout << "Koniec programu" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement