RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
Help me out with this function!

0 Members and 1 Guest are viewing this topic.

***
Rep:
Level 57
Noun. The act of illuminating.
Contestant - GIAW 9
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 for:
Code: [Select]
  #---------------------------------------------------------#
  # * 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

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?

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Most Mature Member2012 Favorite Staff Member2012 Best RPG Maker User (Scripting)2012 Best MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
I have a method for drawing lines in my Bitmap Addons script. Perhaps that could help.

***
Rep:
Level 57
Noun. The act of illuminating.
Contestant - GIAW 9
Thanks! I will check it out.