Coder Social home page Coder Social logo

geometric-queries's Introduction

Description

My Haskell implementation for some geometric queries, starting from this Geometric Intro problem set by Katherine. Haskell code is in PointsAndLines.hs, Polygons.hs and Gradients.hs.

It also contains a JavaScript visualizer for some of the functions.

current progress

Mostly working on gradients for now. Here's what I have so far, although imperfect and sometimes oscillates.

  • input a point p and a segment xy:
    • move p to decrease distance between the two
    • move xy to decrease distance between the two
    • rotate xy around its midpoint to decrease distance
  • input a point p, a segment xy, and a point c:
    • rotate xy around c to decrease distance between xy and p
  • input a point p and a pair of segments (call it s for here)
    • move p to decrease distance between p and s
    • move s to decrease distance between p and s

Usage for the visualizer

Get it running

(note: haven't tried running the binary from a computer without GHC installed.)

From command line, run ./main to start the server. Then in a browser go to localhost:9200

If any change is made to the Haskell code, need to recompile with ghc --make main.hs to update the change in visualizer.

Alternatively, load main.hs into ghci and run the main function.

p.s. Please don't change any function names in Haskell code.

Usage

To make elements: press 1, 2 or 3 to start recording.

1: record a point. Click on where to place it.
2: record a segment. Click twice to specify its endpoints.
3: record a polygon. Click to specify its vertices in order. When done, press c (close).

Or can also manually enter elements from the input field at the bottom. Format: points separated by commas, each point in form [x,y].

1 point: point;
2 points: segment;
3 or more points: polygon

When not recording, click on an element to toggle selection. If clicked at places where elements overlap, it toggles all elements underneath. Selected elements are listed in order on the left of the canvas.

Press d to delete selected elements.

(global var) mode = 0: visualize queries

Click on a function on top left (whose required arguments are also listed).

Press 0 to feed currently selected list of elements as input to the selected function. Order of arguments matter (otherwise it crashes).

A screenshot

Note: some results show up as new elements added onto the canvas, but some only show as text output in the console.

(global var) mode = 1: gradient descent (default)

Click on a function on top left (whose required arguments are also listed).

Press v to make 1 step of descent.

Press b to descend until either (1) it converges, or (2) reaches max number of steps (500).

If input is a point and a segment, or a point of two segments, can also press g to show a rough graph of distance on rotation of segment(s).

A screenshot

More screenshots from visualizer

screenshot 2

screenshot 3

screenshot 4

screenshot 5

Tools used

Scotty

p5.js

geometric-queries's People

Contributors

miyehn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

stjordanis

geometric-queries's Issues

Issue with using Fay

Known functions that don't compile:

isInfinite :: RealFloat a => a -> Bool
atan2 :: a -> a -> a

  • atan2 y x gives the angle of vector (x, y) from the origin, result in range -pi to pi

A weird runtime error:

In file Polygons.hs, with type Point = (Double, Double) in scope:

Problematic code (that works in ghci but not after compiled by Fay):

-- returns the closest point to p on a polygon's boundary
closestPointGP :: Polygon -> Point -> Point
closestPointGP pts p = let
  points = map (closestPointPS p) (getSegments pts) --closest point to each segment
  in foldl (\p1 p2->if (dist p p1)<(dist p p2) then p1 else p2) (points!!0) points

Error when trying to use this code:
prob-screenshot

Workaround code (which is logically the same as problematic code but uglier)

-- returns the closest point to p on a polygon's boundary
closestPointGP :: Polygon -> Point -> Point
closestPointGP pts p = let 
  points = map (closestPointPS p) (getSegments pts)--closest point to each segment
  closer p1 p2 = (dist p p1) < (dist p p2)
  in foldl
    (\(x1,y1) (x2,y2)->if closer (x1,y1) (x2,y2) then (x1,y1) else (x2,y2)) 
    (points!!0) points

Correct output:
prob-screenshot

Next step

Find a way to run haskell code directly instead of compiling to javascript first. Directions to look: loading and generating local json files, building a small server

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.