Skip to content

Commit

Permalink
package name geometry (TheAlgorithms#474)
Browse files Browse the repository at this point in the history
* feat: bitwise min

* fix: bitwise min, change for vararg

* fix: change min tests

* fix: benchmark for bitwise

* fix: rename tests

* fix: add value param

* fix: change condition

* feat: added description to some functions

* Updated Documentation in README.md

* fix: change descriptions

* Updated Documentation in README.md

* Updated Documentation in README.md

* Updated Documentation in README.md

* feat: abs algo

* Updated Documentation in README.md

* fix: add comment

* Updated Documentation in README.md

* fix: new comment

* Updated Documentation in README.md

* fix: rename func and remove package

* Updated Documentation in README.md

* Update math/abs.go

Co-authored-by: Taj <[email protected]>

* Updated Documentation in README.md

* Update math/binary/abs.go

Co-authored-by: Taj <[email protected]>

* Updated Documentation in README.md

* fix: rename func and move binary test

* add package description and add function name to comment

* Updated Documentation in README.md

* rename Intercept method to YIntercept

* Updated Documentation in README.md

* fix strings

* Updated Documentation in README.md

Co-authored-by: Rak Laptudirm <[email protected]>
Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Taj <[email protected]>
  • Loading branch information
4 people authored Mar 17, 2022
1 parent b875ba7 commit 3c64010
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,22 +382,25 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.

---

##### Package geometry contains geometric algorithms

---
##### Functions:

1. [`Distance`](./math/geometry/straightlines.go#L17): Calculates the shortest distance between two points.
2. [`Intercept`](./math/geometry/straightlines.go#L36): Calculates the Y-Intercept of a line from a specific Point.
3. [`IsParallel`](./math/geometry/straightlines.go#L41): Checks if two lines are parallel or not.
4. [`IsPerpendicular`](./math/geometry/straightlines.go#L46): Checks if two lines are perpendicular or not.
5. [`PointDistance`](./math/geometry/straightlines.go#L52): Calculates the distance of a given Point from a given line. The slice should contain the coefficiet of x, the coefficient of y and the constant in the respective order.
6. [`Section`](./math/geometry/straightlines.go#L23): Calculates the Point that divides a line in specific ratio. DO NOT specify the ratio in the form m:n, specify it as r, where r = m / n.
7. [`Slope`](./math/geometry/straightlines.go#L31): Calculates the slope (gradient) of a line.
1. [`Distance`](./math/geometry/straightlines.go#L18): Distance calculates the shortest distance between two points.
2. [`IsParallel`](./math/geometry/straightlines.go#L42): IsParallel checks if two lines are parallel or not.
3. [`IsPerpendicular`](./math/geometry/straightlines.go#L47): IsPerpendicular checks if two lines are perpendicular or not.
4. [`PointDistance`](./math/geometry/straightlines.go#L53): PointDistance calculates the distance of a given Point from a given line. The slice should contain the coefficiet of x, the coefficient of y and the constant in the respective order.
5. [`Section`](./math/geometry/straightlines.go#L24): Section calculates the Point that divides a line in specific ratio. DO NOT specify the ratio in the form m:n, specify it as r, where r = m / n.
6. [`Slope`](./math/geometry/straightlines.go#L32): Slope calculates the slope (gradient) of a line.
7. [`YIntercept`](./math/geometry/straightlines.go#L37): YIntercept calculates the Y-Intercept of a line from a specific Point.

---
##### Types

1. [`Line`](./math/geometry/straightlines.go#L12): No description provided.
1. [`Line`](./math/geometry/straightlines.go#L13): No description provided.

2. [`Point`](./math/geometry/straightlines.go#L8): No description provided.
2. [`Point`](./math/geometry/straightlines.go#L9): No description provided.


---
Expand Down
19 changes: 10 additions & 9 deletions math/geometry/straightlines.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Package geometry contains geometric algorithms
package geometry

import (
"math"
)

// Defines a point with x and y coordinates.
// Point defines a point with x and y coordinates.
type Point struct {
X, Y float64
}
Expand All @@ -13,12 +14,12 @@ type Line struct {
P1, P2 Point
}

// Calculates the shortest distance between two points.
// Distance calculates the shortest distance between two points.
func Distance(a, b *Point) float64 {
return math.Sqrt(math.Pow(a.X-b.X, 2) + math.Pow(a.Y-b.Y, 2))
}

// Calculates the Point that divides a line in specific ratio.
// Section calculates the Point that divides a line in specific ratio.
// DO NOT specify the ratio in the form m:n, specify it as r, where r = m / n.
func Section(p1, p2 *Point, r float64) Point {
var point Point
Expand All @@ -27,27 +28,27 @@ func Section(p1, p2 *Point, r float64) Point {
return point
}

// Calculates the slope (gradient) of a line.
// Slope calculates the slope (gradient) of a line.
func Slope(l *Line) float64 {
return (l.P2.Y - l.P1.Y) / (l.P2.X - l.P1.X)
}

// Calculates the Y-Intercept of a line from a specific Point.
func Intercept(p *Point, slope float64) float64 {
// YIntercept calculates the Y-Intercept of a line from a specific Point.
func YIntercept(p *Point, slope float64) float64 {
return p.Y - (slope * p.X)
}

// Checks if two lines are parallel or not.
// IsParallel checks if two lines are parallel or not.
func IsParallel(l1, l2 *Line) bool {
return Slope(l1) == Slope(l2)
}

// Checks if two lines are perpendicular or not.
// IsPerpendicular checks if two lines are perpendicular or not.
func IsPerpendicular(l1, l2 *Line) bool {
return Slope(l1)*Slope(l2) == -1
}

// Calculates the distance of a given Point from a given line.
// PointDistance calculates the distance of a given Point from a given line.
// The slice should contain the coefficiet of x, the coefficient of y and the constant in the respective order.
func PointDistance(p *Point, equation [3]float64) float64 {
return math.Abs(equation[0]*p.X+equation[1]*p.Y+equation[2]) / math.Sqrt(math.Pow(equation[0], 2)+math.Pow(equation[1], 2))
Expand Down
4 changes: 2 additions & 2 deletions math/geometry/straightlines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func TestIntercept(t *testing.T) {
p := Point{0, 3}
var slope float64 = -5
var wantedIntercept float64 = 3
var calculatedIntercept float64 = Intercept(&p, slope)
var calculatedIntercept float64 = YIntercept(&p, slope)
if calculatedIntercept != wantedIntercept {
t.Fatalf("Failed to calculate Intercept.")
t.Fatalf("Failed to calculate YIntercept.")
}
}

Expand Down

0 comments on commit 3c64010

Please sign in to comment.