Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define COORD(buffer, x, y, line) buffer[y*line+x]
  6. typedef enum {
  7. DIRECTION_DEFAULT = 1,
  8. DIRECTION_REVERSE = -1
  9. } DIRECTION_T;
  10.  
  11. void drawTriangle(char *buffer, int bufferLine, int x, int y, int height, DIRECTION_T direction, char ch) {
  12. int i, j, k;
  13. for (i=y; i != y + (direction * height); i += direction) {
  14. j = abs(i - y);
  15. for (k = x - j; k <= x + j; k++) {
  16. COORD(buffer, k, i, bufferLine) = ch;
  17. }
  18. }
  19. }
  20.  
  21. void drawRecusiveEmptyTriangle(char *buffer, int bufferLine, int x, int y, int height) {
  22.  
  23. drawTriangle(buffer, bufferLine, x, y + height/2+height/2-1, height/2, DIRECTION_REVERSE, ' ');
  24.  
  25. if (height / 2 == 1)
  26. return;
  27. drawRecusiveEmptyTriangle(buffer, bufferLine, x, y, height/2);
  28. drawRecusiveEmptyTriangle(buffer, bufferLine, x - height/2, y + height/2, height/2);
  29. drawRecusiveEmptyTriangle(buffer, bufferLine, x + height/2, y + height/2, height/2);
  30.  
  31. }
  32.  
  33. void printBuffer(char *buffer, int height, int width) {
  34. int i, j;
  35. for (i = 0; i<height; i++) {
  36. for (j = 0; j<width; j++) {
  37. putchar(COORD(buffer, j, i, width));
  38. }
  39. putchar('\n');
  40. }
  41. }
  42.  
  43. int main() {
  44. int line;
  45. int width;
  46. scanf("%d", &line);
  47. width = line * 2 -1;
  48. char *buffer = (char *)malloc(sizeof(char) * line * width);
  49. memset(buffer, ' ', sizeof(char) * line * width);
  50.  
  51. drawTriangle(buffer, width, width/2, 0, line, DIRECTION_DEFAULT, '*');
  52. drawRecusiveEmptyTriangle(buffer, width, width/2, 0, line);
  53.  
  54. printBuffer(buffer, line, width);
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement