Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. //
  2. //  Copyright © 2017 Bartosz Gronkiewicz. All rights reserved.
  3. //
  4.  
  5. #include <iostream>
  6. #include <cmath>
  7. using namespace std;
  8.  
  9. int main(int argc, char * argv[]) {
  10.  
  11.     const int sizeX = 50;
  12.     const int sizeY = 20;
  13.  
  14.     int x1, x2, y1, y2, xb, xt, yb, yt;
  15.     int dx, dy;
  16.     char tab[sizeY][sizeX];
  17.  
  18.     if (argc == 5){
  19.         x1 = atoi(argv[1]);
  20.         y1 = atoi(argv[2]);
  21.         x2 = atoi(argv[3]);
  22.         y2 = atoi(argv[4]);
  23.        
  24.         if(y1 < y2) {
  25.             xb = x1;
  26.             yb = y1;
  27.             xt = x2;
  28.             yt = y2;
  29.         }
  30.         else {
  31.             xb = x2;
  32.             yb = y2;
  33.             xt = x1;
  34.             yt = y1;
  35.         }
  36.        
  37.         dx = xt - xb;
  38.         dy = yt - yb;
  39.        
  40.         //rysowanie układu współrzędnych
  41.         for (int i = 0; i < sizeY; i++){
  42.             for (int j = 0; j < sizeX; j++){
  43.                 if (i == sizeY/2){
  44.                     tab[i][j] = '-';
  45.                 }
  46.                 else if (j == sizeX/2){
  47.                     tab[i][j] = '|';
  48.                 }
  49.                 else{
  50.                     tab[i][j] = ' ';
  51.                 }
  52.                 if (i == sizeY/2 && j == sizeX/2){
  53.                     tab[i][j] = 'o';
  54.                 }
  55.             }
  56.         }
  57.         //algorytm punktu środkowego
  58.         if(abs(dx) > dy && dx > 0) {
  59.             cout << "Pierwszy przypadek" << endl;
  60.             double D = 2.0*(double)dy - (double)dx;
  61.             int x = xb;
  62.             int y = yb;
  63.             while (x <= xt) {
  64.                 if(D > 0) {
  65.                     D = (double)D + 2.0*(double)dy;
  66.                 }
  67.                 else {
  68.                     D = 2.0*((double)dy-(double)dx);
  69.                     y++;
  70.                 }
  71.                 cout << "(" << x << ", " << y << ")" << endl;
  72.                 tab[sizeY/2 - y][sizeX/2 + x] = 'x';
  73.                 x++;
  74.             }
  75.         }
  76.         //algorytm naiwny
  77.         else if(abs(dx) < dy && dx > 0) {
  78.             cout << "Drugi przypadek" << endl;
  79.             int y = yb;
  80.             int x = 0;
  81.             while (y <= yt) {
  82.                 x = 0;
  83.                 x = (int)round((double)((double)dx/(double)dy) * (y - yb) + xb);
  84.                 cout << "(" << x << ", " << y << ")" << endl;
  85.                 tab[sizeY/2 - y][sizeX/2 + x] = 'x';
  86.                 y++;
  87.             }
  88.         }
  89.         //algorytm naiwny
  90.         else if(abs(dx) < dy && dx < 0) {
  91.             cout << "Trzeci przypadek" << endl;
  92.             int y = yb;
  93.             int x = 0;
  94.             while (y <= yt) {
  95.                 x = 0;
  96.                 x = (int)round((double)((double)dx/(double)dy) * (y - yb) + xb);
  97.                 cout << "(" << x << ", " << y << ")" << endl;
  98.                 tab[sizeY/2 - y][sizeX/2 + x] = 'x';
  99.                 y++;
  100.             }
  101.         }
  102.         //algorytm naiwny
  103.         else if(abs(dx) > dy && dx < 0) {
  104.             cout << "Czwarty przypadek" << endl;
  105.             int x = xb;
  106.             int y = 0;
  107.             while (x >= xt) {
  108.                 y = 0;
  109.                 y = (int)round((double)((double)dy/(double)dx) * (x - xb) + yb);
  110.                 cout << "(" << x << ", " << y << ")" << endl;
  111.                 tab[sizeY/2 - y][sizeX/2 + x] = 'x';
  112.                 x--;
  113.             }
  114.         }
  115.        
  116.         for (int i = 0; i < 20; i++){
  117.             for (int j = 0; j < 50; j++){
  118.                 cout << tab[i][j];
  119.             }
  120.             cout << '\n';
  121.         }
  122.     }
  123.     cout << "Koniec programu" << endl;
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement