Main Menu
  • Welcome to The RPG Maker Resource Kit.

Help me out with this function!

Started by Illumination™, March 19, 2012, 11:57:01 PM

0 Members and 1 Guest are viewing this topic.

Illumination™

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?

modern algebra

I have a method for drawing lines in my Bitmap Addons script. Perhaps that could help.

Illumination™