计算三角形外接圆的函数:

Three Point Circle

If we want to build new silos, then we need to make more formal and useful plots of land. Our topographer only marks a few points on the map and thinks that is good enough. It doesn't give us the coordinates which all our robots use, just a handwritten note. Paper notes! The nerve.

Through any three points that do not exist on the same line, there lies a unique circle. The points of this circle are represented in a string with the coordinates like so:

(x1,y1),(x2,y2),(x3,y3)

Where x1,y1,x2,y2,x3,y3 are digits.

You should find the circle for the three given points, such that the circle lies through these point and return the result as a string with the equation of the circle. In a Cartesian coordinate system (with an X and Y axis), the circle with central coordinates of (x0,y0) and radius of r can be described with the following equation:

(x-x0)^2+(y-y0)^2=r^2

Where x0y0r are decimal numbers rounded to two decimal points. Remove extraneous zeros and all decimal points, they are not necessary. For rounding, use the standard mathematical rules.

Input: Coordinates as a string.

Output: The equation of the circle as a string.

#答案
def circle_equation(note):
note = list(eval(note))
x1 = note[0][0]
x2 = note[1][0]
x3 = note[2][0]
y1 = note[0][1]
y2 = note[1][1]
y3 = note[2][1]
a = ((x3-x2)**2+(y3-y2)**2)**0.5
b = ((x3-x1)**2+(y3-y1)**2)**0.5
c = ((x1-x2)**2+(y1-y2)**2)**0.5
p = 0.5*(a+b+c)
s = (p*(p-a)*(p-b)*(p-c))**0.5
x = ((y2 - y1) * (y3 * y3 - y1 * y1 + x3 * x3 - x1 * x1) - (y3 - y1) * (y2 * y2 - y1 * y1 + x2 * x2 - x1 * x1)) / (
2 * (x3 - x1) * (y2 - y1) - 2 * ((x2 - x1) * (y3 - y1)));
y = ((x2 - x1) * (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1) - (x3 - x1) * (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1)) / (
2 * (y3 - y1) * (x2 - x1) - 2 * ((y2 - y1) * (x3 - x1)));
r = a*b*c/(4*s)
r = round(r, 2)
if x.is_integer():
x = int(x)
if y.is_integer():
y = int(y)
if r.is_integer():
r = int(r)
string = '(x-' + str(x) + ')^2+(y-' + str(y) + ')^2=' + str(r) + '^2'
return string #调试结果
print(circle_equation("(2,2),(6,2),(2,6)"))
print(circle_equation("(3,7),(6,9),(9,7)"))
print(circle_equation("(2,2),(6,2),(2,6)") == "(x-4)^2+(y-4)^2=2.83^2")
print(circle_equation("(3,7),(6,9),(9,7)") == "(x-6)^2+(y-5.75)^2=3.25^2")

【Checkio Exercise】Three Point Circle的更多相关文章

  1. 【Checkio Exercise】Probably Dice

    题目: Probably Dice Battle is full of randomnesses. You should observe randomness in a controlled sett ...

  2. 【Checkio Exercise】Robot Sort

    Robot Sort All of the refined ingots should be sorted by size in each lot while passing by on a conv ...

  3. 【递推】【卡特兰数】CODEVS 3134 Circle

    新GET了一种卡特兰数的应用…… 在一个圆上,有2*K个不同的结点,我们以这些点为端点,连K条线段,使得每个结点都恰好用一次.在满足这些线段将圆分成最少部分的前提下,请计算有多少种连线的方法. 不会证 ...

  4. 【Python CheckiO 题解】SP

    题目描述 [Speech Module]:输入一个数字,将其转换成英文表达形式,字符串中的所有单词必须以一个空格字符分隔. [输入]:一个数字(int) [输出]:代表数字的英文字符串(str) [前 ...

  5. 【Python基础】lpthw - Exercise 44 继承与组合

    一.继承 原则:大部分使用继承的场合都可以用组合取代或者简化,而多重继承则需要不惜一切的避免. 1. 什么是继承 继承:用于指明一个类的大部分或者全部功能都是从一个父类获得的.此时,父类的实例的所有动 ...

  6. 【POJ 1981】Circle and Points(已知圆上两点求圆心坐标)

    [题目链接]:http://poj.org/problem?id=1981 [题意] 给你n个点(n<=300); 然后给你一个半径R: 让你在平面上找一个半径为R的圆; 这里R=1 使得这个圆 ...

  7. 【Web动画】SVG 线条动画入门

    通常我们说的 Web 动画,包含了三大类. CSS3 动画 javascript 动画(canvas) html 动画(SVG) 个人认为 3 种动画各有优劣,实际应用中根据掌握情况作出取舍,本文讨论 ...

  8. 【设计模式 - 4】之原型模式(Prototype)

    1      模式简介 原型模式的定义:通过复制一个现有的对象(原型)来得到一个相似的对象. 原型模式的UML图如下图所示: 从上图中可以看到,所有的对象实体类都是继承自一个Prototype的父类, ...

  9. 【设计模式 - 6】之桥接模式(Bridge)

    1      模式简介 举个例子,人.车和公路是三个维度,人开着车在公路上行驶,就是将这三个维度进行了关联.人分男人(Man)和女人(Woman),车分小轿车(Car)和公共汽车(Bus),公路分市区 ...

随机推荐

  1. 【中间件安全】WebSphere安全加固规范

    1. 适用情况 适用于使用WebSphere进行部署的Web网站. 2. 技能要求 熟悉WebSphere安装部署,熟悉WebSphere常见漏洞利用方式,并能针对站点使用WebSphere进行安全加 ...

  2. iOS 调试大法

    本文转载至 http://www.jianshu.com/p/d19e19a91071 0.笨办法 看变量.对象?NSLog+重新编译运行:改某对象?改源码+重新编译运行:隔离某个方法?在方法中写 r ...

  3. Golang 笔记 4 defer、error、panic

    一.defer语句 defer语句仅能被放置在函数或方法中.它由关键字defer和一个调用表达式组成.这里的表达式所表示的既不能是对Go语言内建函数的调用也不能是对Go语言标准库代码包unsafe中的 ...

  4. Spark Streaming之dataset实例

    Spark Streaming是核心Spark API的扩展,可实现实时数据流的可扩展,高吞吐量,容错流处理. bin/spark-submit --class Streaming /home/wx/ ...

  5. Visual Studio Many Projects in One Solution VS中多工程开发

    在用VS开发项目的时候,我们可以将不同的模块拆分开来,分别建立单独的Project来开发调试,整个放到一个Solution中就行了.这样做的好处是结构清晰,每个功能模块可以单独调试,而不用编译整个So ...

  6. 最大团&优化

    貌似咕了三个半月了(gym101915里一道),今天又遇到一道(cf1105E),就学了学惹. 最大团定义:图上取尽可能多的点,这些点构成一个完全图. 最大独立集:图上取尽可能多的点,任意两点间不连接 ...

  7. python中文分词库——pynlpir

    安装 pip install pynlpir import pynlpir #加载包 pynlpir.open() #加载nlpir的库,这步是必须的 #否则会出现 段错误/段转储 segment f ...

  8. python全栈开发 * 线程锁 Thread 模块 其他 * 180730

    一,线程Thread模块1.效率更高(相对于进程) import time from multiprocessing import Process from threading import Thre ...

  9. 队列模拟基本操作I

    看到这道题,第一个想法就是“搜索”!“回溯”!的确,这种思路是很正确的,BFS和DFS都可以来解决: #include <cstdlib> #include <cstring> ...

  10. java+js实现展示本地文件夹下的所有图片demo[申明:来源于网络]

    java+js实现展示本地文件夹下的所有图片demo[申明:来源于网络] 地址:http://blog.csdn.net/allgis/article/details/46364875