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

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. 微软消息队列-MicroSoft Message Queue(MSMQ)队列的C#使用

    目录 定义的接口 接口实现 建立队列工厂 写入队列 获取消息 什么是MSMQ Message Queuing(MSMQ) 是微软开发的消息中间件,可应用于程序内部或程序之间的异步通信.主要的机制是:消 ...

  2. 浅谈前端JavaScript编程风格

    前言 多家公司和组织已经公开了它们的风格规范,详细可參阅jscs.info,以下的内容主要參考了Airbnb的JavaScript风格规范.当然还有google的编程建议等编程风格 本章探讨怎样使用E ...

  3. phpstorm 破解

    http://idea.lanyus.com/ https://www.imsxm.com/jetbrains-license-server.html [  http://www.activejetb ...

  4. Go 用JSON加载表格数据

    支持热重载reload,但会有一些问题,下面注释有写 package table import ( "runtime/debug" ) //IntArray int类型数组 typ ...

  5. 深度学习入门之Mnist

    参看Deep learning from scratch,学习到反向传播网络后,把网络调通了,但是训练后损失函数减小,准确率没有变化,和瞎猜一样,是为什么呢?只有在看看各层缺少什么,关键是我的参数和书 ...

  6. J - Network of Schools

    来源poj1236 A number of schools are connected to a computer network. Agreements have been developed am ...

  7. phpcms 自定义方法

    路径:\install_package\phpcms\libs\functions\extention.func.php <?php //输入栏目ID获取父级栏目名称 function catp ...

  8. Git查看两个版本之间修改了哪些文件

    gdiff 63e3b647d55fcc643e793e650c893be8601719b1 548cdaf01dbc2f08d1ca0b697a24afe512b75a2f --stat git l ...

  9. 剑指offer——python【第14题】链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点. 思路 注意,看清楚,是输出节点,而不是输出节点值 可以先求出链表总长度,然后正向遍历得到第n个节点 解答 class Solution: def ...

  10. js读取解析JSON类型数据【申明:来源于网络】

    js读取解析JSON类型数据[申明:来源于网络] 地址:http://blog.csdn.net/sunhuaqiang1/article/details/47026841