Notice: fwrite(): Write of 483 bytes failed with errno=28 No space left on device in /home/rmrk/domains/rmrk.net/public_html/Sources/Cache/APIs/FileBased.php on line 96

Notice: fwrite(): Write of 59 bytes failed with errno=28 No space left on device in /home/rmrk/domains/rmrk.net/public_html/Sources/Cache/APIs/FileBased.php on line 96

Notice: fwrite(): Write of 1714 bytes failed with errno=28 No space left on device in /home/rmrk/domains/rmrk.net/public_html/Sources/Cache/APIs/FileBased.php on line 96

Notice: fwrite(): Write of 44 bytes failed with errno=28 No space left on device in /home/rmrk/domains/rmrk.net/public_html/Sources/Cache/APIs/FileBased.php on line 96

Notice: fwrite(): Write of 8192 bytes failed with errno=28 No space left on device in /home/rmrk/domains/rmrk.net/public_html/Sources/Cache/APIs/FileBased.php on line 96
Print Page - Help me out with this function!

The RPG Maker Resource Kit

RMRK RPG Maker Creation => RPG Maker General => General Scripting => Topic started by: Illumination™ on March 19, 2012, 11:57:01 PM

Title: Help me out with this function!
Post by: Illumination™ on March 19, 2012, 11:57:01 PM
So I am trying to write a simple 3D engine, or at least a geometry engine and I am starting at the very basics. I need a function to draw lines from one point to another.

I complete this task by creating a variable for x and y, initialized as the first point coordinates. I then move these variables towards the second point in a loop, while drawing points everywhere they come.

This worked fine for lines in which the distance on the X axis is equal to that one the Y axis, which sucks. Basically because it would move one closer on both axes for each pixel.

So my function ended up like this: (Spoiler because the function is so ugly and might be offensive to young children :P)

[spoiler]
  #---------------------------------------------------------#
  # * Draw Line
  #---------------------------------------------------------#
  def draw_line(x1, y1, x2, y2, color)
    # Assign agent variables
    x = x1
    y = y1
    # Find out the relation between the axis
    horizontal = (x-x2).abs
    vertical = (y-y2).abs
    # Find the amount of bend required
    horizontal_bend = (horizontal) / [horizontal, vertical].max
    vertical_bend = (vertical) / [horizontal, vertical].max
    horizontal_time = 0
    vertical_time = 0
    # While We are not yet there
    while(x != x2 && y != y2)
      # Up bend one
      horizontal_time += 1
      vertical_time += 1
      # Set back after a round
      horizontal_time = 0 if horizontal_time == horizontal_bend
      vertical_time = 0 if vertical_time == vertical_bend
      # Bend it
      if horizontal_time == 0
        x += horizontal_bend if x < x2
        x -= horizontal_bend if x > x2
      end
      if vertical_time == 0
        y += vertical_bend if x < y2
        y -= vertical_bend if x > y2
      end
      # Move ordinary
      x += 1 if x < x2
      x -= 1 if x > x2
      y += 1 if y < y2
      y -= 1 if y > y2
      # Draw it
      self.set_pixel(x, y, color)
    end
  end
[/spoiler]

So one: It is long and chunky, more so than I think it needs to.
Two: It doesn't even work, it now draws some weird-ass lines. It seems it now ends up on a second point which is further away than it should be.

So any tips on making it work and preferably in a more elegant way?
Title: Re: Help me out with this function!
Post by: modern algebra on March 20, 2012, 12:02:44 AM
I have a method for drawing lines in my Bitmap Addons (http://rmrk.net/index.php/topic,32286.0.html) script. Perhaps that could help.
Title: Re: Help me out with this function!
Post by: Illumination™ on March 20, 2012, 12:16:53 AM
Thanks! I will check it out.