# How do I... figure out how much of a level is "covered"

Get help using Construct 2

### » Thu Jan 08, 2015 4:17 pm

The basic idea is, in one mode you place cameras in a level so you can view the area remotely later. I'm trying to require that the whole area be covered by the cameras before moving on. Unfortunately, I can't figure out a good way to do this.

It may be that this is more of a math problem than a Construct 2 problem, in which case, I'm sorry.

From my screenshot, you can see two cameras, of seven in the level, and the minimap showing the location of all seven cameras and their 'square of influence.' These cameras are placed by the player. As the squares can overlap, how do I figure out how much of the level is covered- since it's not just the amount of area each camera *can* cover times the number of cameras in use.
You do not have the required permissions to view the files attached to this post.
B
11
S
4
Posts: 152
Reputation: 1,236

Bump
B
8
S
2
G
1
Posts: 96
Reputation: 782

### » Fri Jan 09, 2015 10:44 pm

Could you use a invisible grid of smaller squares and then using "is not overlapping" divide the uncovered grid squares from the grid squares overlapping and then multply by 100 to get a percent?
B
11
S
2
Posts: 40
Reputation: 680

### » Sat Jan 10, 2015 5:15 am

@Unnatural20
@david7457

Since I needed to take a break from my current project, and enjoy interesting challenges, I worked up a demo and posted a Capx example:

Calculating Rectangular Coverage Area

Hopefully it has everything you need.
B
64
S
19
G
8
Posts: 341
Reputation: 12,225

@zatyka

Nice
B
11
S
2
Posts: 40
Reputation: 680

### » Sat Jan 10, 2015 6:41 pm

This is really cool! But I have a question: in event 11, you set TestRectangleRight to (Xcoordinates.At(loopindex("X")))+(Xcoordinates.At(loopindex("X")+1)-Xcoordinates.At(loopindex("X")))
Doesn't this always evaluate to the same as Xcoordinates.At(loopindex("X")+1)?

Approximately the same expression appears for TestRectangleBottom.
B
11
S
4
Posts: 152
Reputation: 1,236

### » Sat Jan 10, 2015 7:09 pm

Unnatural20 wrote:in event 11, you set TestRectangleRight to (Xcoordinates.At(loopindex("X")))+(Xcoordinates.At(loopindex("X")+1)-Xcoordinates.At(loopindex("X")))
Doesn't this always evaluate to the same as Xcoordinates.At(loopindex("X")+1)?

You are correct. The overly complex expression was left over from a slightly different calculation method. I've updated the Capx. Thanks for pointing it out.
B
64
S
19
G
8
Posts: 341
Reputation: 12,225

### » Sat Jan 10, 2015 8:59 pm

@Zatyka

Great example! I would have headed towards some sort of tilemap based solution, but this is far more elegant and efficient.
B
45
S
10
G
10
Posts: 1,106
Reputation: 9,232

### » Sat Jan 10, 2015 9:48 pm

TiAm wrote:@Zatyka

Great example! I would have headed towards some sort of tilemap based solution, but this is far more elegant and efficient.

It is super elegant and very efficient!
I added it to a .capx with just about 10 events, given that the existence of the objects and their overlap was already there.

Now, though, I must decide; since my cameras can potentially rotate... do I prohibit rotation? Do I ignore their rotation, and simply estimate coverage? (if this algorithm finds a square rotated 45 degrees, for example, it will treat it as a square covering twice the volume.)
Do I allow rotation up to a certain number of degrees?
B
11
S
4
Posts: 152
Reputation: 1,236

### » Sat Jan 10, 2015 10:49 pm

@TiAm
Thanks

@Unnatural20
The algorithm uses the fill object's bounding box, which are always rectangular regardless of the objects rotation. To answer your question directly, yes, the algorithm would double the covered area:

If you need precise coverage calculations for rectangles (or other polygon) that allows for rotation, you'd need to break the entire area into triangles based on each rectangle's vertices, and test if each triangle overlaps the fill object. It's exponentially more difficult, though not impossible.

If you only need semi-accurate calculations, you could do as others have suggested and create a small sprite, or high-density tilemap, and loop through the entire area checking for overlaps. You'd probably end up balancing accuracy vs. efficiency.
B
64
S
19
G
8
Posts: 341
Reputation: 12,225