-
Notifications
You must be signed in to change notification settings - Fork 27
/
190.go
56 lines (46 loc) · 1.25 KB
/
190.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// UVa 190 - Circle Through Three Points
package main
import (
"fmt"
"io"
"math"
"os"
)
type point struct{ x, y float64 }
func sign(condition bool) string {
if condition {
return "-"
}
return "+"
}
func output(out io.Writer, p []point) {
a1, b1 := p[1].x-p[0].x, p[1].y-p[0].y
c1 := p[1].x*p[1].x - p[0].x*p[0].x + p[1].y*p[1].y - p[0].y*p[0].y
a2, b2 := p[2].x-p[0].x, p[2].y-p[0].y
c2 := p[2].x*p[2].x - p[0].x*p[0].x + p[2].y*p[2].y - p[0].y*p[0].y
d := (b1*c2 - b2*c1) / (a1*b2 - a2*b1)
e := (a2*c1 - a1*c2) / (a1*b2 - a2*b1)
x0 := -d / 2
y0 := -e / 2
r := math.Sqrt((p[1].x-x0)*(p[1].x-x0) + (p[1].y-y0)*(p[1].y-y0))
fmt.Fprintf(out, "(x %s %.3f)^2 + (y %s %.3f)^2 = %.3f^2\n", sign(x0 > 0), math.Abs(x0), sign(y0 > 0), math.Abs(y0), r)
f := -p[1].x*p[1].x - p[1].y*p[1].y - p[1].x*d - p[1].y*e
fmt.Fprintf(out, "x^2 + y^2 %s %.3fx %s %.3fy %s %.3f = 0\n", sign(d < 0), math.Abs(d), sign(e < 0), math.Abs(e), sign(f < 0), math.Abs(f))
}
func main() {
in, _ := os.Open("190.in")
defer in.Close()
out, _ := os.Create("190.out")
defer out.Close()
here:
for {
p := make([]point, 3)
for i := range p {
if _, err := fmt.Fscanf(in, "%f%f", &p[i].x, &p[i].y); err != nil {
break here
}
}
output(out, p)
fmt.Fprintln(out)
}
}