-
Notifications
You must be signed in to change notification settings - Fork 41
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
Created 'MonotonePolygon' #95
Conversation
Excellent! To rehash the algorithm:
I'll add some type-signatures and tests to your branch. |
Could you tick "Allow edits from maintainers" if you haven't already. Here's a guide: https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/allowing-changes-to-a-pull-request-branch-created-from-a-fork |
Box is checked. Thanks for giving me a starting point, I'll get started tomorrow |
For random Rational points, see this code: granularity :: Integer
granularity = 10000000
-- Random point between screenTop/screenBottom.
genPoint :: RandomGen g => Rand g (Point 2 Rational)
genPoint = do
x <- liftRand $ randomR (0, granularity)
y <- liftRand $ randomR (0, granularity)
pure $ Point2
((x % granularity) * screenHeight - screenTop)
((y % granularity) * screenHeight - screenTop) It has to be in the |
Also, you can use the |
Your solution is very close to being correct. It just has to be monadic and use randomMonotone :: RandomGen g => Int -> Vector 2 Rational -> Rand g (SimplePolygon () Rational)
randomMonotone nVertices direction = do
-- 1, skip 2 in this function bc `direction` is given
points <- replicateM nVertices genPoint
-- 3
let min = Data.Geometry.Polygon.Core.minimumBy (cmpExtreme direction) points
max = Data.Geometry.Polygon.Core.maximumBy (cmpExtreme direction) points
-- rest of your code here. |
Another hint: There's a built-in function for testing which side a point is on. toTheLeft :: Point 2 r -> Bool
toTheLeft x = ccw min max x == CCW You can then use |
This code: polygon = SimplePolygon $ Data.CircularSeq.fromList (min : leftHalf : max : rightHalf) Can be written as: polygon = fromPoints ([min] ++ leftHalf ++ [max] ++ rightHalf) Let me know if I'm micro-managing too much and you'd rather want some peace and quiet. :) |
No you're fine, I appreciate the advice. I am pretty new to haskell and
very new to this codebase so it is helpful to see these shortcuts. I've got
some other responsibilities to tend to to wrap up this work week but I'll
get back to this very soon and incorporate your suggestions.
…On Tue, Jan 12, 2021 at 10:18 PM David Himmelstrup ***@***.***> wrote:
This code:
polygon = SimplePolygon $ Data.CircularSeq.fromList (min : leftHalf : max : rightHalf)
Can be written as:
polygon = fromPoints ([min] ++ leftHalf ++ [max] ++ rightHalf)
Let me know if I'm micro-managing too much and you'd rather want some
peace and quiet. :)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#95 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AH4DYV7JHIRB5EJNDJ56BXDSZUGJFANCNFSM4V4BPIVQ>
.
|
Sorry for all the commits. I am getting an error while building |
What error do you get when building hgeometry-ipe? As a workaround (i.e. so that locally you can at least properly compile stuff), you can probably also remove hgeometry-ipe from the cabal.project file as well as disable tests. Not really an ideal solution though. |
I will push some visualization code to make debugging easier. |
Pushed. |
Ah, I made a mistake in |
I love it! Your code works really well: random_monotone.mp4 |
I added two TODO items to your original post. |
Hey that's pretty cool! I'll work on those changes, I like the idea of a generalized |
Oh yeah, |
I've deleted some of the code I copy&pasted into your module at the start of the PR. It's been replaced by a better and more standard way of generating random points. Your code is unchanged except for a few type signatures. |
Looks good, are we ready to move it to the proper directory and submit the PR? |
Yes, everything looks good. I'll move the module, write a QuickCheck test, and mark the PR as ready. |
Alright, looks like everything is in order:
I'll go ahead and merge this PR. Thank you so much, @1ndy. You've improved HGeometry. I hope we'll see more algorithms from you in the future. |
Thanks for all your help!! |
I'd like to attempt random monotone polygon generation
TODO:
randomMonotone
should generate a randomVector
and callrandomMonotoneDirected
.randomMonotoneDirected
should generate a set of random points and callrandomMonotoneFrom
. Splitting up the functions like this makes it a lot easier to test them.