1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

[SOLVED] Line drawing Algorithm

Discussion in 'Programming' started by Sabnock, Jan 17, 2020.

  1. Sabnock

    Sabnock Member

    Joined:
    Jul 21, 2016
    Posts:
    364
    Looking for a good line drawing algorithm.

    I want to be able to per pixel colour a line so i can animate colour along the length of the line which GML does seem to cover

    i have looked on line and seen a few options but wanted to get advice on what was best for quick, efficient game use

    options i have looked at are:


    any help would be appreciated
     
  2. GmlProgrammer

    GmlProgrammer Member

    Joined:
    Oct 12, 2016
    Posts:
    28
    You can pass a vertex with two points as a pr_linelist, then you can use any texture you want for your line without rewriting the algorithm.
     
  3. GmlProgrammer

    GmlProgrammer Member

    Joined:
    Oct 12, 2016
    Posts:
    28
    Code:
    ///@param x1
    ///@param y1
    ///@param x2
    ///@param y2
    ///@param color
    
    
    var xmin = min(argument0,argument2);
    var xmax = max(argument0,argument2);
    var ymin = min(argument1,argument3);
    var ymax = max(argument1,argument3);
    var x1 = argument0;
    var x2 = argument2;
    var y1 = argument1;
    var y2 = argument3;
    var color = argument4;
    
    
    var xdist = xmax-xmin;
    var ydist = ymax-ymin;
    
    if(xdist == 0 && ydist == 0){
        draw_point_color(xmax,ymax,color);
    }
    
    
    if(xdist <= ydist){
       
        for(var yy = ymin; yy <= ymax; yy++){
            var xx = round(x2+(x2-x1)*(yy-ymax)/ydist);
            if(y1 > y2) xx = round(x1+(x1-x2)*(yy-ymax)/ydist);
            draw_point_color(xx,yy,color);
        }
       
    
    }else{
    
        for(var xx = xmin; xx <= xmax; xx++){
            var yy = round(y2+(y2-y1)*(xx-xmax)/xdist);
            if(x1 > x2) yy = round(y1+(y1-y2)*(xx-xmax)/xdist);
            draw_point_color(xx,yy,color);
        }
    
    }
    Also, you can use this simple script I wrote some time ago. You can modify it to add different colors to each point
     
  4. Sabnock

    Sabnock Member

    Joined:
    Jul 21, 2016
    Posts:
    364
    This looks exactly what i need. I will try it when i get home.

    Thank you very much

    :)
     
    GmlProgrammer likes this.
  5. Sabnock

    Sabnock Member

    Joined:
    Jul 21, 2016
    Posts:
    364
    this works great. thank you again
     

Share This Page