2

Consider the following:

Currently, I'm programming a very basic dungeon generator. Right now, it works like this:

  1. Generate an int[][] hyperarray of arbritrary lengths.

  2. Place rooms at random coordinates in this hyperarray (we'll call it maze for now.) using a for loop that fills the heigth and width of the room with 1's in maze. For game engine purposes 0 means not traversable (a wall.) and 1 means traversable by the player/enemies.

  3. Start a perfect maze generator at 0,0 and run until all the space inbetween the rooms is filled with corridors.

  4. Connect rooms to corridors, remove dead ends, you're left with a system of interconnected rooms.

Like this

dungeon

Now I'd like to jazz up these rooms because they're just flat rectangles. What I now need to do is find a way to generate a polygon inside the int hyperarray (coordinates will probably do fine, this isn't the problem) and then fill the space of the polygon in the hyperarray with 1's (the part I'm having trouble with).

Ascor
  • 33
  • 4

1 Answers1

0

I'm trying to figure out how to "draw lines within a integer array" right now. what i found out was, i could draw a line from one point to another, by doing the following: find the width and height of point 1 and point 2 (absolute value of x1-x2 and y1-y2, divide width by (height-1) and from point 1 to point 2, along the y axis move towards point 2 by single steps, and on the x axis by width / height steps. Example:

01000
01000
00100
00100
00010
00001

here i have a line from (1,0) as (x,y) to (4,5) if i take the width and height i end up with width = 3 height = 5 if i divide 3/5 i end up with 0.6 and so for every single step along the y axis, i take a 0.6 step along the x axis (and floor the resulting value, since we can only use integers as positions) and end up with the koordinates

(1,0)
(1.6,1) -> (1,1)
(2.2,2) -> (2,2)
(2.8,3) -> (2,3)
(3.4,4) -> (3,4)
(4,5) ->   (4,5)

with this you could draw those lines for every coordinate pair of the polygon using this line function

x = x1 + (|(y1-y)| * ( |(x1-x2)| / |(y1-y2)| ) * sign(x2-x1))

where x is the current x of every iteration, following the line along the y axis and x1 beeing your start x position, x2 beeing your end position y beeing the current y of every iteration (the actual counter during the loop to be specific) y1 and y2 beeing the start y and end y positions repectively. and sign(x2-x1) to help figure out, wether you are going to the left or right from the start position.

with this you should be able to draw the outline of your polygon, and hopefully it should become easier to fill it afterwards.

i hope this helps you a bit regards

EDIT: we draw lines for every consecutive pair of coordinates, not EVERY SINGLE pair. meaning if you would draw a polygon from points p1,p2,p3,p4,p5 you would draw a line from p1 to p2, p2 to p3, p3 to p4, p4 to p5 and p5 to p1

just a quick sidenote

Alan
  • 949
  • 8
  • 26