【Checkio Exercise】Three Point Circle
计算三角形外接圆的函数:
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 x0, y0, r 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的更多相关文章
- 【Checkio Exercise】Probably Dice
题目: Probably Dice Battle is full of randomnesses. You should observe randomness in a controlled sett ...
- 【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 ...
- 【递推】【卡特兰数】CODEVS 3134 Circle
新GET了一种卡特兰数的应用…… 在一个圆上,有2*K个不同的结点,我们以这些点为端点,连K条线段,使得每个结点都恰好用一次.在满足这些线段将圆分成最少部分的前提下,请计算有多少种连线的方法. 不会证 ...
- 【Python CheckiO 题解】SP
题目描述 [Speech Module]:输入一个数字,将其转换成英文表达形式,字符串中的所有单词必须以一个空格字符分隔. [输入]:一个数字(int) [输出]:代表数字的英文字符串(str) [前 ...
- 【Python基础】lpthw - Exercise 44 继承与组合
一.继承 原则:大部分使用继承的场合都可以用组合取代或者简化,而多重继承则需要不惜一切的避免. 1. 什么是继承 继承:用于指明一个类的大部分或者全部功能都是从一个父类获得的.此时,父类的实例的所有动 ...
- 【POJ 1981】Circle and Points(已知圆上两点求圆心坐标)
[题目链接]:http://poj.org/problem?id=1981 [题意] 给你n个点(n<=300); 然后给你一个半径R: 让你在平面上找一个半径为R的圆; 这里R=1 使得这个圆 ...
- 【Web动画】SVG 线条动画入门
通常我们说的 Web 动画,包含了三大类. CSS3 动画 javascript 动画(canvas) html 动画(SVG) 个人认为 3 种动画各有优劣,实际应用中根据掌握情况作出取舍,本文讨论 ...
- 【设计模式 - 4】之原型模式(Prototype)
1 模式简介 原型模式的定义:通过复制一个现有的对象(原型)来得到一个相似的对象. 原型模式的UML图如下图所示: 从上图中可以看到,所有的对象实体类都是继承自一个Prototype的父类, ...
- 【设计模式 - 6】之桥接模式(Bridge)
1 模式简介 举个例子,人.车和公路是三个维度,人开着车在公路上行驶,就是将这三个维度进行了关联.人分男人(Man)和女人(Woman),车分小轿车(Car)和公共汽车(Bus),公路分市区 ...
随机推荐
- .NET Core 2.0 获取完整的URL
在之前的ASP.NET中,可以通过 Request.Url.AbsoluteUri 获取,但在ASP.NET Core没有这个实现,请问如何获取呢? 方法一:先引用“using Microsoft.A ...
- 解决ubuntu下音乐播放器Rhythmbox乱码问题
两种解决方法 第一种是修改用户的环境变量 1.先打开主文件夹 cd /home/user #user是你的用户名然后编辑用户环境 2.sudo gedit .profile在打开的文件中添加: exp ...
- 125、Android样式的开发(转载)
Android样式的开发:drawable汇总篇 http://android.jobbole.com/82117/Android样式的开发:layer-list篇 http://android.jo ...
- selenium 操作过程中,元素标红高亮的两种实现方式
在使用selenium时,动作元素标红高亮,在定位问题时相当好用,有以下二种方法可以实现 一.使用js将元素属性修改 这也是网上大部分的实现方式,但有时候会有点小问题,代码如下: 只写其实某一段函数 ...
- 11.17 luffycity(7)完结
2018-11-17 15:59:01 路飞项目已经完结!后面已是flask的学习!然后还有十几天的课程等回学校再看 明天归校!! 开始全面整理自己学习的知识,整理博客!还有好多面试题!233333 ...
- J - Network of Schools
来源poj1236 A number of schools are connected to a computer network. Agreements have been developed am ...
- thinkphp5中使用PHPExcel(转载)
thinkphp5中可以使用composer来获取第三方类库,使用起来特别方便,例如:可是使用composer下载PHPMailer,think-captcha(验证码)等等…… 接下来说一下怎么使用 ...
- vue-cli创建第一个项目(用git bash解决上下键移动选择问题)
我电脑是windows:(nodejs已经有了) 1 下载vue-cli cmd 打开命令行,或者是gitbash.最好是用cnpm比较快. 2 创建项目: dos命令,cd 你的希望创建的文件夹 ...
- 无网络 使用pip安装mxnet
# 在有网络的同系统机器上运行以下命令:pip download mxnet# 目前mxnet版本为1.3.0,执行后当前目录得到以下文件: # . # ├── certifi--py2.py3-no ...
- Caused by: java.lang.ClassNotFoundException: Could not load requested class :XXX.XXX.XXX 异常处理
在ssh整合中:出现的异常 Exception sending context initialized event to listener instance of class org.springfr ...