标签:out close find 圆心 ret math except pid exti
题意:给出 4 个点,问第4个点是否在前三个点形成的外接圆外。
tags: 会有精度问题,上 java 。。
1】公式
已知三点坐标,求外接圆圆心坐标与半径。
//已知三点坐标,求外接圆圆心坐标与半径。 x=((y2-y1)*(y3*y3-y1*y1+x3*x3-x1*x1)-(y3-y1)*(y2*y2-y1*y1+x2*x2-x1*x1))/(2.0*((x3-x1)*(y2-y1)-(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.0*((y3-y1)*(x2-x1)-(y2-y1)*(x3-x1))); r^2=(x1-a)*(x1-a)+(y1-b)*(y1-b);
//package project1; import java.util.*; import java.math.*; import java.util.Scanner; public class Main { //Test2 { public static void main(String[] argv) throws Exception { Scanner sc = new Scanner(System.in); MathContext mc = new MathContext(7, RoundingMode.HALF_DOWN); int T = sc.nextInt(); for(int cas=1; cas<=T; ++cas) { BigDecimal x1=sc.nextBigDecimal(), y1=sc.nextBigDecimal(), x2=sc.nextBigDecimal(), y2=sc.nextBigDecimal(); BigDecimal x3=sc.nextBigDecimal(), y3=sc.nextBigDecimal(), x4=sc.nextBigDecimal(), y4=sc.nextBigDecimal(); BigDecimal x, y, r; BigDecimal a1=y2.subtract(y1), a2=y3.multiply(y3), a3=y1.multiply(y1), a4=x3.multiply(x3), a5=x1.multiply(x1); BigDecimal a6 = (y2.subtract(y1)).multiply(y3.pow(2).subtract(y1.pow(2)).add(x3.pow(2)).subtract(x1.pow(2))); BigDecimal a7 = (y3.subtract(y1)).multiply(y2.pow(2).subtract(y1.pow(2)).add(x2.pow(2)).subtract((x1.pow(2)))); a1=BigDecimal.valueOf(2); a2=x3.subtract(x1); a3=y2.subtract(y1); a4=x2.subtract(x1); a5=y3.subtract(y1); BigDecimal a8 = a1.multiply((a2).multiply(a3).subtract(a4.multiply(a5))); x = a6.subtract(a7).divide(a8); a6 = (x2.subtract(x1)).multiply(x3.pow(2).subtract(x1.pow(2)).add(y3.pow(2)).subtract(y1.pow(2))); a7 = (x3.subtract(x1)).multiply(x2.pow(2).subtract(x1.pow(2)).add(y2.pow(2)).subtract((y1.pow(2)))); a1=BigDecimal.valueOf(2); a2=y3.subtract(y1); a3=x2.subtract(x1); a4=y2.subtract(y1); a5=x3.subtract(x1); a8 = a1.multiply(a2.multiply(a3).subtract(a4.multiply(a5))); y = a6.subtract(a7).divide(a8); a1 = (x1.subtract(x).pow(2)); a2 = (y1.subtract(y).pow(2)); r = a1.add(a2); a1 = (x.subtract(x4).pow(2)); a2 = (y.subtract(y4).pow(2)); BigDecimal dis = a1.add(a2); //System.out.println(x+" "+y+" "+r+" "+dis+"-----"); if(dis.compareTo(r) > 0) System.out.println("Accepted"); else System.out.println("Rejected"); } } } /* 已知三点坐标,求外接圆圆心坐标与半径。 x=((y2-y1)*(y3*y3-y1*y1+x3*x3-x1*x1)-(y3-y1)*(y2*y2-y1*y1+x2*x2-x1*x1))/(2.0*((x3-x1)*(y2-y1)-(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.0*((y3-y1)*(x2-x1)-(y2-y1)*(x3-x1))); r^2=(x1-a)*(x1-a)+(y1-b)*(y1-b); */
2】函数
//三点找一圆 Circle findCircle2(P p1, P p2, P p3) { double A1, A2, B1, B2, C1, C2, temp; A1 = p1.x - p2.x; B1 = p1.y - p2.y; C1 = (p1.x*p1.x - p2.x*p2.x + p1.y*p1.y - p2.y*p2.y) / 2; A2 = p3.x - p2.x; B2 = p3.y - p2.y; C2 = (p3.x*p3.x - p2.x*p2.x + p3.y*p3.y - p2.y*p2.y) / 2; temp = A1*B2 - A2*B1; Circle CD; CD.center.x = (C1*B2 - C2*B1) / temp; CD.center.y = (A1*C2 - A2*C1) / temp; CD.radius = sqrtf((CD.center.x - p1.x)*(CD.center.x - p1.x) + (CD.center.y - p1.y)*(CD.center.y - p1.y)); return CD; }
标签:out close find 圆心 ret math except pid exti
原文地址:http://www.cnblogs.com/sbfhy/p/7554419.html