Golang. Point belong to the circle?


The last example on Pythagoras’ theorem is quite simple. Let’s take a problem from the same topic, but more complicated. Suppose we have a circle in the center of x-axis and y-axis (i.e. center at coordinates 0,0). Let’s write a test for finding an arbitrary point inside the circle.

Here again Pythagoras will help us. In the general case, the formula is as follows:


(x - center_x)^2 + (y - center_y)^2 < radius^2

But our conditions are a little simpler, the center of the circle is at coordinates 0,0; that’s why the code is like this:

package main

import (
	"fmt"
	"math"
)

func main() {

	r := 11.0

	var x, y float64 = -7.77, -7.77

	if math.Pow(x, 2)+math.Pow(y, 2) <= math.Pow(r, 2) {
		fmt.Println("Inside")
	} else {
		fmt.Println("Outside")
	}
}

This entry was posted in Go (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.