Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polygon function to draw visible area #10

Open
wants to merge 4 commits into
base: temp-master
Choose a base branch
from

Conversation

Felecarpp
Copy link

A visibility function to get the polygon of visible area from a list of segments (walls).
There are three new functions in vector.lua used by visibility function: intersection, alignment and polar_lt
I put a demonstration script, unit test script and performance timer script.

feat(vector): orientation & intersection

refactor(vector): use exponentiation operator

refactor(vector): rename function orientation -> alignment

feat(vector): polar_lt func to compare angle of vectors
Copy link
Owner

@HDictus HDictus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I very much appreciate the additon of unit tests. However, what isn't clear to me from this is what visibles.polygon is actually for. The demo does not make it much clearer I'm afraid. Cna you explain it further?

@@ -119,11 +148,11 @@ function vector:toPolar()
end

function vector:len2()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

best to make a separate pull request for these tweaks to keep changes atomic

@HDictus
Copy link
Owner

HDictus commented Nov 4, 2022

I very much appreciate the additon of unit tests. However, what isn't clear to me from this is what visibles.polygon is actually for. The demo does not make it much clearer I'm afraid. Cna you explain it further?

Ah I feel silly, it is for calculating line of sight. This seems like something handy, but I'm not sure it belongs in hump rather than in a standalone library.
Either way I'll give it another review so you can use the feedback whether we merge or you make your own library.

3, 3, 3, -1,
-1, -1, -1, 3,
-1, -1, 3, -1
}),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In tests it is often a good idea to be explicit: so pass vector:zero here and elsewhere you rely on it as default.

visible.lua Outdated
return vector.polar_lt(a.start, b.start, center)
end

function visible.polygon(segments, center)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tricky thing with asking for a Vector object here is that it then is compatible only with Vector and not vector-light. People have their reasons for preferring vector-light. You could make it receive two arguments for center: center_or_x and y. Then you can interperet center_or_x as a vector if y is nil, as the x component of a vector from vector-light if y is not nil.

@@ -0,0 +1,32 @@
local visible = require("visible")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

benchmark_visible.lua would be a more conventional filename for this

-- < 0 -> counterclockwise
-- = 0 -> colinear
-- > 0 -> clockwise
local function alignment(a, b, c)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll need a bit more thorough a description of what this funtion represents, for example in the docs. From the name alone its purpose is not obvious.

local vis = require("visible")


function testOrientation()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding tests. The tests for vector functions should have their own file, tests/vector.lua

visible.lua Outdated
do
local next_endpoints = {}
local missing_stops = {}
for i = 1, #segments, 4 do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In each loop here you clearly do a distinct operation. Name them and create local functions for them. it makes the code more modular, easier to build on and easier to read.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, you might name this one collect_endpoints

visible.lua Outdated
-- return concave (frequently) polygon
if center == nil then center = vector.zero end
local endpoints = {}
do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This do seems to just form a block for no reason. I didn't actually know you could put a do statement alone without causing a syntaxerror.

visible.lua Outdated
vector.alignment(epj.start, epj.stop, epi.start)
* vector.alignment(epj.start, epj.stop, epi.stop) <= 0 then
local intersec =
vector.intersection(epi.start, epi.stop, epj.start, epj.stop)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting is a bit odd here

lu.assertTrue(vec.alignment(vec(0, 1), vec(1, 1), vec(2, 1)) == 0)
end

function testIntersection()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like the kind of function where you will want to test edge cases. does it work when they exactly intersect at a point? What does it return when they do not intersect at all?

@Felecarpp
Copy link
Author

Thanks for these reviews. I will see to them but not for several months.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants