Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
muktihari committed Sep 5, 2024
0 parents commit c47daba
Show file tree
Hide file tree
Showing 9 changed files with 1,417 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: CI

on:
push:
branches: ["master"]

pull_request:
branches: ["master"]

permissions: {}

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

- name: Set up Go
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
with:
go-version: "stable"

- name: Test
run: go test -v -cover -coverprofile=coverage.coverprofile ./...

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
21 changes: 21 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Hikmatulloh Hari Mukti

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Carto

![GitHub Workflow Status](https://github.com/muktihari/carto/workflows/CI/badge.svg)
[![Go Reference](https://pkg.go.dev/badge/github.com/muktihari/carto.svg)](https://pkg.go.dev/github.com/muktihari/carto)
[![CodeCov](https://codecov.io/gh/muktihari/carto/branch/master/graph/badge.svg)](https://codecov.io/gh/muktihari/carto)
[![Go Report Card](https://goreportcard.com/badge/github.com/muktihari/carto)](https://goreportcard.com/report/github.com/muktihari/carto)

This project hosts the implementation of various Cartography algorithms in Go.

## Import

```sh
go get github.com/muktihari/carto
```

## List of algorithms:

- [Ramer–Douglas–Peucker](https://w.wiki/B6U3) [[github.com/muktihari/carto/rdp](./rdp/rdp.go)]: Simplifies a curve by reducing the number of points in a curve composed of line segments, resulting in a similar curve with fewer points. It's a zero-alloc implementation that performs efficiently by reslicing the input **points**.

![rdp](./docs/rdp_sample.svg)

Benchmark (n = 1000 points):

```js
goos: linux; goarch: amd64; pkg: github.com/muktihari/carto/rdp
cpu: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
BenchmarkSimplify-4 6428 161746 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/muktihari/carto/rdp 1.065s
```
108 changes: 108 additions & 0 deletions docs/rdp_sample.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/muktihari/carto

go 1.20.0

require github.com/google/go-cmp v0.6.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
61 changes: 61 additions & 0 deletions rdp/rdp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Package rdp host the implementation of [Ramer–Douglas–Peucker algorithm](https://w.wiki/B6U3) algorithm.
package rdp

import "math"

// Point represents coordinates in 2D plane.
type Point struct {
X, Y float64 // X and Y represent coordinates (e.g Latitude and Longitude)
Index int // [Optional] Index reference to the origin data.
}

// Simplify simplifies a curve by reducing the number of points in a curve composed of line segments,
// resulting in a similar curve with fewer points. Ref: [Ramer–Douglas–Peucker algorithm](https://w.wiki/B6U3).
//
// Note: The resulting slice is a reslice of given points (it shares the same underlying array) for efficiency.
// It works similar to append, so the input points should not be used after this call, use only the returned value.
func Simplify(points []Point, epsilon float64) []Point {
if len(points) < 2 {
return points
}

var (
index int
maxDist float64
first = points[0]
last = points[len(points)-1]
)

for i := range points {
d := perpendicularDistance(points[i], first, last)
if d > maxDist {
maxDist = d
index = i
}
}

if maxDist <= epsilon {
return append(points[:0], first, last)
}

firstHalf, lastHalf := points[:index], points[index:]

return append(Simplify(firstHalf, epsilon), Simplify(lastHalf, epsilon)...)
}

// perpendicularDistance calculates the perpendicular distance from a point to a line segment
func perpendicularDistance(p, start, end Point) float64 {
if start.X == end.X && start.Y == end.Y {
return euclidean(start, end)
}
numerator := math.Abs((end.Y-start.Y)*p.X - (end.X-start.X)*p.Y + end.X*start.Y - end.Y*start.X)
denominator := euclidean(start, end)
return numerator / denominator
}

// euclidean calculates the distance between two points.
func euclidean(p1, p2 Point) float64 {
x := p2.X - p1.X
y := p2.Y - p1.Y
return math.Sqrt(x*x + y*y)
}
Loading

0 comments on commit c47daba

Please sign in to comment.