Advertisement
metalni

OOP Labs 1 Tocka

May 30th, 2020
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef struct tocka2D{
  5.     float x, y;
  6. } tocka2D;
  7.  
  8. typedef struct tocka3D{
  9.     float x, y, z;
  10. } tocka3D;
  11.  
  12. float rastojanie(tocka2D a, tocka2D b){
  13.     float base, distance;
  14.     base=((b.x-a.x)*(b.x-a.x))+((b.y-a.y)*(b.y-a.y));
  15.     distance=sqrt(base);
  16.     return distance;
  17. }
  18.  
  19. float rastojanie3D(tocka3D a, tocka3D b){
  20.     float base, distance;
  21.     base=((b.x-a.x)*(b.x-a.x))+((b.y-a.y)*(b.y-a.y)+((b.z-a.z)*(b.z-a.z)));
  22.     distance=sqrt(base);
  23.     return distance;
  24. }
  25.  
  26. /* We use triangle's area to determine whether the points lay on the same line, if the area is 0, they lay on the same line, otherwise, they don't  */
  27. int ista_prava(tocka2D a, tocka2D b, tocka2D c){
  28.     float area;
  29.     area = (a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y))/2;
  30.     if(area==0.0)
  31.         return 1;
  32.     else
  33.         return 0;
  34. }
  35.  
  36. int main(void) {
  37.     float x1, y1, x2, y2;
  38.  
  39.     scanf("%f %f", &x1, &y1);
  40.     scanf("%f %f", &x2, &y2);
  41.    
  42.     tocka2D t1 = { x1, y1 };
  43.     tocka2D t2 = { x2, y2 };
  44.     printf("%.2f\n", rastojanie(t1, t2));
  45.    
  46.     float z1, z2;
  47.     scanf("%f %f", &z1, &z2);
  48.     tocka3D t3 = {x1, y1, z1};
  49.     tocka3D t4 = {x2, y2, z2};
  50.     printf("%.2f\n", rastojanie3D(t3, t4));
  51.    
  52.     tocka2D t5 = {z1, z2};
  53.     printf("%d\n", ista_prava(t1, t2, t5));
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement