Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <mlx.h>
  5.  
  6. typedef struct s_data
  7. {
  8. void *mlx_ptr;
  9. void *mlx_win;
  10. } t_data;
  11.  
  12. typedef struct s_point
  13. {
  14. int x;
  15. int y;
  16. } t_point;
  17.  
  18. void swap(t_point *p1, t_point *p2)
  19. {
  20. t_point temp;
  21.  
  22. temp = *p1;
  23. *p1 = *p2;
  24. *p2 = temp;
  25. }
  26.  
  27. int ft_abs(int num)
  28. {
  29. return (num < 0 ? -num : num);
  30. }
  31.  
  32. void draw_line(t_data *data, t_point p0, t_point p1)
  33. {
  34. int dx;
  35. int dy;
  36. int err[2];
  37. int sx;
  38. int sy;
  39.  
  40. dx = ft_abs(p1.x - p0.x);
  41. dy = -ft_abs(p1.y - p0.y);
  42. err[0] = dx + dy;
  43. sx = p0.x < p1.x ? 1 : -1;
  44. sy = p0.y < p1.y ? 1 : -1;
  45. while(1)
  46. {
  47. mlx_pixel_put(data->mlx_ptr, data->mlx_win, p0.x, p0.y, 0xffffff);
  48. if (p0.x == p1.x && p0.y == p1.y)
  49. break;
  50. err[1] = 2 * err[0];
  51. if (err[1] >= dy && (err[0] += dy))
  52. p0.x += sx;
  53. if (err[1] <= dx && (err[0] += dx))
  54. p0.y += sy;
  55. }
  56. }
  57.  
  58. int main(void)
  59. {
  60. t_data data;
  61. t_point p1;
  62. t_point p2;
  63.  
  64. p1.x = 100;
  65. p1.y = 100;
  66.  
  67. p2.x = 500;
  68. p2.y = 200;
  69. if ((data.mlx_ptr = mlx_init()) == NULL)
  70. return (EXIT_FAILURE);
  71. if ((data.mlx_win = mlx_new_window(data.mlx_ptr, 640, 480, "Hello world")) == NULL)
  72. return (EXIT_FAILURE);
  73. draw_line(&data, p2, p1);
  74. mlx_loop(data.mlx_ptr);
  75. return (EXIT_SUCCESS);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement