`
从此醉
  • 浏览: 1089821 次
  • 性别: Icon_minigender_1
  • 来自: US
社区版块
存档分类
最新评论

POJ 3130 计算几何 java

阅读更多

How I Mathematician Wonder What You Are!

问题描述 :

After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a mathematician uses a big astronomical telescope and lets his image processing program count stars. The hardest part of the program is to judge if shining object in the sky is really a star. As a mathematician, the only way he knows is to apply a mathematical definition of stars.

The mathematical definition of a star shape is as follows: A planar shape F is star-shaped if and only if there is a point C ∈ F such that, for any point P ∈ F, the line segment CP is contained in F. Such a point C is called a center of F. To get accustomed to the definition let’s see some examples below.

The first two are what you would normally call stars. According to the above definition, however, all shapes in the first row are star-shaped. The two in the second row are not. For each star shape, a center is indicated with a dot. Note that a star shape in general has infinitely many centers. Fore Example, for the third quadrangular shape, all points in it are centers.

Your job is to write a program that tells whether a given polygonal shape is star-shaped or not.

 

输入:

The input is a sequence of datasets followed by a line containing a single zero. Each dataset specifies a polygon, and is formatted as follows.

n  
x1 y1
x2 y2

xn yn

The first line is the number of vertices, n, which satisfies 4 ≤ n ≤ 50. Subsequent n lines are the x- and y-coordinates of the n vertices. They are integers and satisfy 0 ≤ xi ≤ 10000 and 0 ≤ yi ≤ 10000 (i = 1, …, n). Line segments (xi, yi)–(xi + 1, yi + 1) (i = 1, …, n − 1) and the line segment (xn, yn)–(x1, y1) form the border of the polygon in the counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions.

You may assume that the polygon is simple, that is, its border never crosses or touches itself. You may assume assume that no three edges of the polygon meet at a single point even when they are infinitely extended.

 

输出:

For each dataset, output “1” if the polygon is star-shaped and “0” otherwise. Each number must be in a separate line and the line should not contain any other characters.

 

样例输入:

6 
66 13 
96 61 
76 98 
13 94 
4 0 
45 68 
8 
27 21 
55 14 
93 12 
56 95 
15 48 
38 46 
51 65 
64 31 
0

样例输出:

1
0

 

import java.util.Scanner;  
  
public class Main {  
  
    static double x[], y[];  
    static double tx[], ty[];  
    static double txp[], typ[];  
    static int num;  
    static int tnum;  
    static double a, b, c;  
  
    public static void main(String[] args) {  
  
        Scanner scan = new Scanner(System.in);  
  
        while (true) {  
  
            int n = scan.nextInt();  
            if (n == 0)  
                break;  
            num = n;  
  
            x = new double[100];  
            y = new double[100];  
            tx = new double[100];  
            ty = new double[100];  
            txp = new double[100];  
            typ = new double[100];  
  
            for (int i = 0; i < n; i++) {  
                x[i] = scan.nextDouble();  
                y[i] = scan.nextDouble();  
                tx[i + 1] = x[i];  
                ty[i + 1] = y[i];  
            }  
  
            x[n] = x[0];  
            y[n] = y[0];  
            tx[0] = tx[n];  
            ty[0] = ty[n];  
            tx[n + 1] = tx[1];  
            ty[n + 1] = ty[1];  
  
            for (int i = 0; i < n; i++) {  
                a = y[i + 1] - y[i]; //求直线aX+bY+c=0的参数a,b,c   
                b = x[i] - x[i + 1];  
                c = x[i + 1] * y[i] - x[i] * y[i + 1];  
                solve();  
            }  
  
            if (num == 0)  
                System.out.println("0");  
            else  
                System.out.println("1");  
  
        }  
  
    }  
  
    public static void solve() {  
  
        tnum = 0;  
  
        for (int i = 1; i <= num; i++) {  
  
            if (sig(a * tx[i] + b * ty[i] + c) <= 0) {  // 在一侧   
                txp[tnum] = tx[i];  
                typ[tnum++] = ty[i];  
            } else {                                    // 在另一侧   
                if (sig(a * tx[i - 1] + b * ty[i - 1] + c) < 0)  //小于0才会有交点   
                    insert(tx[i - 1], ty[i - 1], tx[i], ty[i]);  
                if (sig(a * tx[i + 1] + b * ty[i + 1] + c )< 0)  
                    insert(tx[i + 1], ty[i + 1], tx[i], ty[i]);  
            }  
        }  
  
        num = tnum;                       //更新  num,tx,ty   
  
        for (int j = 1; j <= num; j++) {  
            tx[j] = txp[j - 1];  
            ty[j] = typ[j - 1];  
        }  
        tx[0] = tx[num];  
        ty[0] = ty[num];  
        tx[num + 1] = tx[1];  
        ty[num + 1] = ty[1];  
  
    }  
  
    private static int sig(double d) {  
        if(d< 1e-10)  
            return -1;  
        else if(d>1e-10)  
            return 1;  
        return 0;  
    }  
   //求两直线交点  其中一条直线  已经表示成ax+by+c=0,  另一直线 是两个点   
    public static void insert(double x1, double y1, double x2, double y2) { 
        double xx = Math.abs(a * x1 + b * y1 + c);  
        double yy = Math.abs(a * x2 + b * y2 + c);  
        txp[tnum] = (x1 * yy + x2 * xx) / (xx + yy);  
        typ[tnum++] = (y1 * yy + y2 * xx) / (xx + yy);  
  
    }  
  
}

 来自:ACM之家

 

www.acmerblog.com

 

分享到:
评论

相关推荐

    北大POJ初级-计算几何学

    【北大POJ初级-计算几何学】是北京大学编程在线判题平台(Problem Online Judge, POJ)上的一系列初级算法题目,主要涉及计算几何领域的知识。这个领域在计算机科学中占有重要地位,因为它在图形处理、游戏开发、...

    凸包练习: POJ 2187(JAVA)

    【标题】"凸包练习: POJ 2187(JAVA)" 是一个关于编程算法的挑战,主要涉及计算机科学中的几何算法。在本问题中,我们关注的是二维平面上的点集及其凸包(Convex Hull)的概念。凸包可以被理解为一个最小的多边形,该...

    POJ2002-Squares

    "Squares"表明这个题目与正方形或者平方数有关,可能涉及到计算几何、数学或数值计算的问题。 【压缩包子文件的文件名称列表】: 1. "POJ2002-Squares.cpp":这是一个C++源代码文件,包含了解决此问题的程序。C++是...

    极角排序:POJ 1696(叉积+深搜)

    这种排序方式对于处理图形问题,尤其是计算几何领域的问题非常有用。在POJ 1696这个编程题目中,很可能需要解决与极角排序相关的问题。POJ(Problem Online Judge)是一个在线的编程竞赛平台,它提供了许多编程题目...

    POJ1328-Radar Installation

    1. **几何计算**:雷达安装可能需要考虑位置的几何特性,比如角度、距离、覆盖范围等。可能需要计算两个雷达之间的夹角,或者确定一个雷达可以覆盖的区域。 2. **最优化问题**:如何在有限的条件下(如预算、空间...

    强大的POJ分类——各类编程简单题及其算法分类

    ### 计算几何学 1. **几何公式**:理解和应用几何原理。 2. **叉积和点积**:用于判断线段相交、计算距离等,如POJ2031和1039。 3. **多边形算法**:处理多边形的面积计算和相关判定,如点在多边形内、多边形是否...

    如何学习ACM,看后受益匪浅

    竞赛中常常涉及到的数学分支包括离散数学、数论、计算几何、线性代数、概率论等。 - **离散数学**:重点在于图论和组合数学,尤其是在图论方面。图论的变化多样,可以与多种数据结构和算法相结合。 - **数论**:以...

    acm poj题目分类介绍 包含一个题解文档

    7. **计算几何**:线段树、凸包、最近点对等问题,涉及到二维空间中的几何形状和性质。 8. **数据结构**:栈、队列、链表、树(二叉树、平衡树如AVL、红黑树等)、堆(最大堆、最小堆)、哈希表等,是解决问题的...

    学习凸包(三):凸包练习 POJ 1113

    在计算机科学领域,凸包(Convex Hull)是一种重要的几何概念,它在各种算法和问题中都有着广泛的应用,比如机器学习、图形学、路径规划等。这篇博客“学习凸包(三): 凸包练习 POJ 1113”显然是关于如何通过编程...

    算法-炮兵阵地(POJ-1185)(包含源程序).rar

    标题中的“炮兵阵地(POJ-1185)”是一个编程竞赛题目,源自国内著名的在线编程平台POJ(编程之美)。这类题目通常要求参赛者编写程序来解决特定的算法问题。在这个案例中,我们可能面临的是一个与数学、策略或者...

    集训全6套练习题-3月9日练习题

    这是一道统计和可视化的问题,需要计算4行字符中每个大写英文字母出现的频率,并以直方图的形式输出。首先,需要遍历输入的字符,统计每个字母出现的次数,然后根据这些统计结果,绘制直方图。在C语言中,可以使用...

    ACM.rar_ACM_acm soj

    数学知识在解决某些特定问题时起着至关重要的作用,如计算几何、组合优化等。 4. **编程语言**:虽然ACM竞赛不限制编程语言,但C++和Java是最常用的语言,因为它们性能优秀且支持模板和面向对象编程,有助于编写...

Global site tag (gtag.js) - Google Analytics