Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. // Draw SVG primitives
  2.  
  3. // Based on: https://www.w3schools.com/graphics/svg_inhtml.asp
  4. // example:
  5. /*
  6. <svg height="100" width="100">
  7. <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
  8. </svg> */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13.  
  14. // User defined functions are in Chapter 6 of your zyBook
  15. void circle ( int cx, int cy, int r, char* color )
  16. {
  17. printf ( "\t<circle cx='%d' cy='%d' r='%d' fill='%s' />\n", cx, cy, r, color) ;
  18. }
  19.  
  20. /*
  21. <svg height="210" width="500">
  22. <line x1="0" y1="0" x2="200" y2="200" style="stroke:rgb(255,0,0);stroke-width:2" />
  23. </svg>
  24. */
  25. void line ( float x1, float y1, float x2, float y2, char* stroke )
  26. {
  27. printf ( "<line x1='%f' y1='%f' x2='%f' y2='%f' style='%s' />\n",
  28. x1, y1, x2, y2, stroke ) ;
  29. }
  30.  
  31. // example from: https://www.w3schools.com/graphics/svg_rect.asp
  32. //
  33. // <rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" />
  34. void rect ( int x, int y, int width, int height, char* color )
  35. {
  36. printf ( "<rect x=%d y=%d width='%d' height='%d' style = 'fill:%s;stroke-width:3;stroke:rgb(0,0,0)'/>\n",
  37. x, y, width, height, color ) ;
  38. }
  39.  
  40. void beginHtmlSVG ( int wide, int high, char* backColor)
  41. {
  42. printf ( "<!DOCTYPE html>\n" ) ;
  43. printf ( "<html>\n" ) ;
  44. printf ( "<body>\n" ) ;
  45.  
  46. printf( "<svg height='%d' width='%d' style='stroke-width: 0px; background-color: %s;'>\n",
  47. high, wide, backColor ) ;
  48. }
  49.  
  50. void endHtmlSVG ( )
  51. {
  52. printf( "</svg>\n") ;
  53. printf ( "</body>\n" ) ;
  54. printf ( "</html>\n" ) ;
  55. }
  56.  
  57. char* getRGBColor ( int red, int green, int blue )
  58. {
  59. static char* svgFillString = "rgb(%d, %d, %d)" ;
  60. static char result[100] = { 0 } ;
  61. sprintf( result, svgFillString, red, green, blue ) ;
  62. return result ;
  63. }
  64.  
  65. int main ( )
  66. {
  67. srand( time ( NULL ) ) ;
  68. // wide, high, background color
  69. beginHtmlSVG ( 1000, 1000, getRGBColor( 128, 0, 128 ) ) ;
  70.  
  71. // Sample code. Add loops and other enhancements here to get
  72. // more and cooler output.
  73.  
  74. int x = 1;
  75. int y = 1;
  76. int width = 100;
  77. int height = 100;
  78. int size = 1;
  79. int red = 2;
  80. int blue = 5;
  81. int green = 1;
  82.  
  83. // col, row, how big, color
  84. for (x = 1; x < 5500; x += 300) {
  85. rect(x, y, width, height, getRGBColor(red, green, blue));
  86. for (y = 1; y < 5500; y += 300) {
  87. rect(x, y, width, height, getRGBColor(red, green, blue));
  88. blue += 2;
  89. red += 1;
  90. }
  91. }
  92.  
  93. for (x = 1; x < 20; x *= 1.6) {
  94. circle(x, y, size, getRGBColor(red, green, blue));
  95. red += 15;
  96. x *= 10;
  97. y *= 1.6;
  98. for (y = 1; y < 2000; y += 5) {
  99. size *= 1.6;
  100. circle(x, y, size, getRGBColor(red, green, blue));
  101. red += 7;
  102. blue -= 10;
  103. green += 2;
  104. x *= 1.6;
  105. y *= 1.6;
  106. }
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. //example using line ( )
  116. // from col, from row, to col, to row, color
  117. //line ( 50.5, 50.75, 75.1, 75.3, "stroke:rgb(0,0,255);stroke-width:2") ;
  118.  
  119. // You can use a site like this: https://www.w3schools.com/colors/colors_picker.asp
  120. // to come up with cool color numbers.
  121.  
  122. // example using rect ( )
  123. // col, row, wide, high, color
  124. // rect ( 200, 200, 150, 50, getRGBColor( 244, 66, 143 ) ) ;
  125.  
  126. endHtmlSVG ( ) ;
  127.  
  128. return 0 ;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement