Advertisement
Guest User

draw_polygon

a guest
Sep 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @desc Uses the scan line algorithm to render a polygon.
  2. /// @param polygon {Integer} The id of the ds_list which stores pairwise positios of verticies.
  3. /// @param outline {Boolean} Draw an outline instead of a filled shape.
  4. /// @author Kat @katsaii
  5. var count = ds_list_size(argument0) div 2;
  6. if (count > 1) {
  7.     if (argument1) {
  8.         // outline
  9.         draw_primitive_begin(pr_linestrip);
  10.         draw_vertex( // last vertex
  11.                 argument0[| (count - 1) * 2 + 0],
  12.                 argument0[| (count - 1) * 2 + 1]);
  13.         for(var i = 0; i < count; i++){
  14.             draw_vertex( // current vertex
  15.                     argument0[| i * 2 + 0],
  16.                     argument0[| i * 2 + 1]);
  17.         }
  18.         draw_primitive_end();
  19.     }else{
  20.         // scanline
  21.         var top = infinity;
  22.         var bottom = -infinity;
  23.         // compile edge table
  24.         var x1 = argument0[| (count - 1) * 2 + 0]; // last vertex
  25.         var y1 = argument0[| (count - 1) * 2 + 1];
  26.         var edges = ds_grid_create(4, count); // every vert has a corresponding edge
  27.         for(var i = 0; i < count; i++){
  28.             var x2 = argument0[| i * 2 + 0];
  29.             var y2 = argument0[| i * 2 + 1];
  30.             // add vertex data to the edge table
  31.             if (y2 < y1){
  32.                 edges[# 0, i] = x1;
  33.                 edges[# 1, i] = y1;
  34.                 edges[# 2, i] = x2;
  35.                 edges[# 3, i] = y2;
  36.             } else {
  37.                 edges[# 0, i] = x2;
  38.                 edges[# 1, i] = y2;
  39.                 edges[# 2, i] = x1;
  40.                 edges[# 3, i] = y1;
  41.             }
  42.             // update last vertex
  43.             x1 = x2;
  44.             y1 = y2;
  45.             // check whether the vertex is the new max/min
  46.             top = min(top, y1);
  47.             bottom = max(bottom, y1);
  48.         }
  49.         // sort the edge table by y-axis
  50.         ds_grid_sort(edges,3,true);
  51.         // draw scan lines
  52.         var line_count = 0;
  53.         top = floor(top);
  54.         bottom = ceil(bottom);
  55.         draw_primitive_begin(pr_linelist);
  56.         for (var yy = top; yy <= bottom; yy++) {
  57.             var intersections = ds_list_create();
  58.             // step through y-axis
  59.             for (var i = 0; i < count; i++) {
  60.                 var x1 = edges[# 0,i];
  61.                 var y1 = edges[# 1,i];
  62.                 var x2 = edges[# 2,i];
  63.                 var y2 = edges[# 3,i];
  64.                 // check for a collision (where y1 < yy)
  65.                 if (y2 > yy) {
  66.                     // no more collisions
  67.                     break;
  68.                 } else if (y1 > yy) {
  69.                     // collision with the edge: find the point of intersection
  70.                     var xx;
  71.                     if (abs(x2 - x1) < 0.01) {
  72.                         // vertical intersection where the gradient is undefined
  73.                         xx = (x2 + x1) * 0.5;
  74.                     } else {
  75.                         // y = mx + c => y + c = mx => x = (y + c)/m
  76.                         var mm = (y2 - y1) / (x2 - x1);
  77.                         var dy = yy - y1;
  78.                         var dx = dy / mm;
  79.                         xx = x1 + dx;
  80.                     }
  81.                     // add x vertex to the vertex list
  82.                     ds_list_add(intersections, xx);
  83.                 }
  84.             }
  85.             // sort verts
  86.             ds_list_sort(intersections, true);
  87.             for(var j = 0; j < ds_list_size(intersections); j++){
  88.                 var xx = intersections[| j];
  89.                 draw_vertex(xx, yy);
  90.                 if (line_count < 1000) {
  91.                     // increment the draw count for the vertex system
  92.                     line_count++;
  93.                 } else {
  94.                     // create a new primitve when the vertex count has reached the threshold
  95.                     draw_primitive_end();
  96.                     draw_primitive_begin(pr_linelist);
  97.                     line_count = 0;
  98.                 }
  99.             }
  100.             ds_list_destroy(intersections);
  101.         }
  102.         draw_primitive_end();
  103.         ds_grid_destroy(edges);
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement