Advertisement
Night_Wood

GMLive IG Logo

Sep 27th, 2017
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define main
  2.  
  3. /*
  4.     @author Nathan Wood (twitter: @night_wood)
  5.     @date   09/28/17
  6.  
  7.     NOTE: This is written for GMLive by YellowAfterlife
  8.         http://yal.cc/r/gml/
  9.        
  10.         You can simply copy + paste into the editor and run it
  11.  
  12.     This draws the Instagram logo using randomly placed dots.
  13. */
  14.  
  15. // Debug variables
  16. DEBUG       = false;
  17.  
  18. // Speed of drawing
  19. DOTS_PER_FRAME  = 360;
  20.  
  21. // Canvas size and background color
  22. WIDTH       = 720;
  23. HEIGHT      = 720;
  24. BACK_COLOR  = c_white;
  25.  
  26. // Drawing constants
  27. SIZE        = 512;      // Size of the logo, SIZE = WIDTH = HEIGHT
  28. DOT_SIZE    = 3;        // Size of the dots (radius)
  29. GRAINY      = false;    // Whether to draw over negative space or not (true = adds noise)
  30. GRAIN_PAD   = 16;       // The padding along the edges of the logo for graininess
  31. ALPHA_RANGE = 0;        // [0, 1]: 0 = no transparency, 1 = fully random transparency
  32.  
  33. // Color constants
  34. //  probably don't want to change these
  35. SATURATION  = 255;      // Colors are made from HSV (hue, saturation, value)
  36. VALUE       = 255;      //      and the hue is based on the distance from origin
  37.  
  38. /*
  39.     - START SCALED CONSTANTS -
  40.    
  41.     Modifying the constants below will mess up the Instagram logo
  42.     They are all a function of the constants defined previously
  43.     Change those instead if you want
  44. */
  45. RADIUS                  = SIZE / 2;
  46.  
  47. CENTER_CIRCLE_RADIUS    = SIZE / 8;
  48. CENTER_RING_RADIUS      = SIZE / 5;
  49.  
  50. FLASH_RADIUS            = (CENTER_RING_RADIUS - CENTER_CIRCLE_RADIUS) / 1.5;
  51. FLASH_DIST              = CENTER_RING_RADIUS;
  52.  
  53. RECTANGLE_RADIUS        = SIZE / 2.6;
  54. RECTANGLE_SIZE          = SIZE / 12;
  55. RECTANGLE_IN_RADIUS     = RECTANGLE_SIZE * 1.8;
  56. RECTANGLE_OUT_RADIUS    = RECTANGLE_SIZE * 2.6;
  57.  
  58. LOGO_ROUND_RADIUS       = RADIUS / 2;
  59.  
  60. MAIN_COLOR_SCALAR       = (480 / SIZE) * 0.10;
  61.  
  62. xstart = WIDTH / 2;
  63. ystart = HEIGHT / 2;
  64.  
  65. /*
  66.     - END SCALED CONSTANTS -
  67. */
  68.  
  69. // Probably don't want to edit anything below this either
  70. room_speed  = 60;
  71. frame       = 0;
  72. canvas      = surface_create(WIDTH, HEIGHT);
  73.  
  74. #define draw
  75.  
  76. // Begin drawing to canvas
  77. surface_set_target(canvas);
  78.  
  79. for(var i = 0; i < DOTS_PER_FRAME; i++) {
  80.    
  81.     // Choose a random point within the image, adding some padding if GRAINY is enabled
  82.     var dx = xstart - (RADIUS + (GRAINY ? GRAIN_PAD : 0))
  83.             + random(SIZE + (GRAINY ? GRAIN_PAD * 2 : 0));
  84.     var dy = ystart - (RADIUS + (GRAINY ? GRAIN_PAD : 0))
  85.             + random(SIZE + (GRAINY ? GRAIN_PAD * 2 : 0));
  86.    
  87.     // Get the distance of the point from the origin and the flash origin
  88.     var dist        = distance(xstart, ystart, dx, dy);
  89.     var dist_flash  = distance(xstart + FLASH_DIST, ystart - FLASH_DIST, dx, dy);
  90.  
  91.     // Create the colors based on the position of the point randomly chosen
  92.     var o_dist  = distance(xstart - (RADIUS * 2/3), ystart - (RADIUS * 2.3), dx, dy);
  93.     var o_hue   = modulo(modulo(o_dist * MAIN_COLOR_SCALAR, 180) - 212, 360);
  94.     var o_color = make_color_hsv(o_hue, SATURATION, VALUE);
  95.    
  96.     var y_dist  = distance(xstart - (RADIUS * 2/5), ystart + RADIUS, dx, dy);
  97.     var y_color = make_color_hsv(35, SATURATION, VALUE);
  98.    
  99.     var color   = o_color;
  100.    
  101.     // Merge the base color with the yellow color if close to the yellow origin
  102.     if(y_dist < o_dist)
  103.         color = merge_color(color, y_color, (o_dist - y_dist) / (o_dist + y_dist));
  104.      
  105.     // The following checks define the negative (white) space in the logo
  106.     // If in center white circle
  107.     if(dist > CENTER_CIRCLE_RADIUS && dist < CENTER_RING_RADIUS)
  108.         color = BACK_COLOR;
  109.     // If in flash circle
  110.     else if(dist_flash < FLASH_RADIUS)
  111.         color = BACK_COLOR;
  112.     // If inside rounded rectangle region
  113.     else if(in_rounded_rectangle2(xstart, ystart,
  114.             RECTANGLE_RADIUS, RECTANGLE_SIZE,
  115.             RECTANGLE_OUT_RADIUS, RECTANGLE_IN_RADIUS, dx, dy))
  116.         color = BACK_COLOR;
  117.     // If inside outer rounded rectangle
  118.     else if(!in_rounded_rectangle(xstart, ystart, RADIUS, LOGO_ROUND_RADIUS, dx, dy))
  119.         color = BACK_COLOR;
  120.        
  121.     // Skip drawing in negative space if GRAINY is not enabled
  122.     if(color == BACK_COLOR && !GRAINY)
  123.         continue;
  124.    
  125.     draw_set_color(color);
  126.     draw_set_alpha(random(RAND_ALPHA) + (1 - RAND_ALPHA));
  127.    
  128.     // Draw smaller points for whitespace
  129.     var point_size = color == BACK_COLOR ? floor(DOT_SIZE / 2) : DOT_SIZE;
  130.    
  131.     draw_circle(dx, dy, point_size <= 1 ? 1 : point_size, false);
  132.        
  133. }
  134.  
  135. // Reset surface target
  136. surface_reset_target();
  137.  
  138. // Draw background color
  139. draw_set_alpha(1);
  140. draw_set_color(BACK_COLOR);
  141. draw_rectangle(0, 0, WIDTH, HEIGHT, false);
  142.  
  143. // Draw canvas to window
  144. draw_surface(canvas, 0, 0);
  145.  
  146. // Draw debug stuff
  147. if(DEBUG) {
  148.     draw_set_color(c_black);
  149.     draw_text(10, 0, ""
  150.         + "#FPS: " + string(fps)
  151.         + "#Dots/Frame: " + string(DOTS_PER_FRAME)
  152.         + "#Dots/Sec: " + string(DOTS_PER_FRAME * fps)
  153.         + "#Frames: " + string(frame)
  154.     );
  155. }
  156.  
  157. frame++;
  158.  
  159. /*
  160.     - START FUNCTIONS -
  161. */
  162.  
  163. #define distance(x1, y1, x2, y2)
  164. /// Returns the distance between 2 points
  165. return sqrt(power(x2 - x1, 2) + power(y2 - y1, 2));
  166.  
  167. #define modulo(a, b)
  168. ///Performs "a modulo b" and supports negative numbers
  169. var r = a % b;
  170. return r < 0 ? r + b : r;
  171.  
  172. #define in_square(x, y, radius, dx, dy)
  173. ///Returns true if point (dx, dy) is in the square centered around (x, y)
  174. return (dx > x - radius && dx < x + radius
  175.     && dy > y - radius && dy < y + radius);
  176.  
  177. #define in_circle(x, y, radius, dx, dy)
  178. ///Returns true if point (dx, dy) is in the circle centered around (x, y)
  179. return distance(x, y, dx, dy) < radius;
  180.  
  181. #define in_rounded_rectangle(x, y, radius, round_radius, dx, dy)
  182. ///Returns true if the point (dx, dy) is in the rounded rectangle centered around (x, y)
  183. /*
  184.     We want to check if dx, dy are within the specified rounded rectangle region
  185.     We do this using a square and 4 circles
  186. */
  187.  
  188. // Check if the point is outside the rectangle
  189. if(!in_square(x, y, radius, dx, dy))
  190.     return false;
  191.  
  192. // Check the rounded edges using circles
  193. /*
  194.     This looks complicated, but basically it checks the 4 corners of the square
  195.         and if the point lies past the origin of the circle it must then also
  196.         lie within the radius of the circle.
  197.    
  198.     h and v are only ever -1 or 1, so we can do a sign comparison instead of
  199.         needing to do multiple unique inequality checks for each corner.
  200. */
  201. for(var h = -1; h <= 1; h += 2) {
  202.     for(var v = -1; v <= 1; v += 2) {
  203.         var origin_x = x + (h * radius) - (h * round_radius);
  204.         var origin_y = y + (v * radius) - (v * round_radius);
  205.        
  206.         if(sign(dx - origin_x) == h && sign(dy - origin_y) == v
  207.             && !in_circle(origin_x, origin_y, round_radius, dx, dy))
  208.             return false;
  209.     }
  210. }
  211.  
  212. // All cases have been considered, it must be inside the rounded rectangle
  213. return true;
  214.  
  215. #define in_rounded_rectangle2(x, y, radius, size, outer_round_radius, inner_round_radius, dx, dy)
  216. ///Returns true if the point (dx, dy) is in the rounded rectangle region centered around (x, y)
  217.  
  218. // Check if the point is outside the outer rounded rectangle
  219. if(!in_rounded_rectangle(x, y, radius, outer_round_radius, dx, dy))
  220.     return false;
  221.  
  222. // Check if the point is not inside the inner rounded rectangle
  223. return !in_rounded_rectangle(x, y, radius - size, inner_round_radius, dx, dy);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement