| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 719 人关注过本帖
标题:求助C语言编程的几何题目~~
只看楼主 加入收藏
xuyao18
Rank: 2
等 级:论坛游民
帖 子:50
专家分:30
注 册:2009-4-27
结帖率:22.22%
收藏
已结贴  问题点数:10 回复次数:9 
求助C语言编程的几何题目~~

题目要求很简单,就是输入M条直线,然后判断这些直线的交点
我用交叉相乘正负判断方向的办法,来做这个题目,也就是下面的CROSS 函数。
程序运行了以后,在判断2条直线的时候,没有问题,但是当我输入3条直线比如(0 0 1 1)(0 1 0 0)(0 0 1 0)的时候,结果就只剩下一个交点了。
请教各位大大,我的程序错在了什么地方。
程序写的不太好,可读性可能不太好。各位见谅哈。


#include <stdio.h>
    int cross(double a,double b,double c,double d,double e,double f,double g,double h)
    {
        double p1[2],p2[2],p3[2],r1,r2,t1[2],t2[2],t3[2];
        p1[0]=c-a;p1[1]=d-b;
        p2[0]=e-a;p2[1]=f-b;
        p3[0]=g-a;p3[1]=h-b;
        r1=(p1[0]*p2[1]-p1[1]*p2[0])*(p1[0]*p3[1]-p1[1]*p3[0]);
        t1[0]=a-c;t1[1]=b-d;
        t2[0]=e-c;t2[1]=f-d;
        t3[0]=g-c;t3[1]=h-d;
        r2=(t1[0]*t2[1]-t1[1]*t2[0])*(t1[0]*t3[1]-t1[1]*t3[0]);
        if(r1<=0&&r2<=0) return 1;
        else return 0;
    }
void main ()
{

    int i,m,flag,sum=0,j,t;
    double a[100][4];
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        for(j=0;j<4;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    if(m=2)
        for(i=0;i<1;i++)
    {
        flag=cross(a[i][0],a[i][1],a[i][2],a[i][3],a[i+1][0],a[i+1][1],a[i+1][2],a[i+1][3]);
        sum+=flag;
    }
    if(m>2)
    {
         for(j=0;j<4;j++)
         {
            a[m][j]=a[0][j];
         }
   
    for(i=0;i<m;i++)
    {
        flag=cross(a[i][0],a[i][1],a[i][2],a[i][3],a[i+1][0],a[i+1][1],a[i+1][2],a[i+1][3]);
        t=flag;
        sum+=t;
    }
    }
    printf("%d",sum);
}

搜索更多相关主题的帖子: C语言 几何 
2009-10-31 22:00
xuyao18
Rank: 2
等 级:论坛游民
帖 子:50
专家分:30
注 册:2009-4-27
收藏
得分:0 
顶一下自己的帖子。 希望高手帮帮。 谢谢了先
2009-10-31 22:07
xuyao18
Rank: 2
等 级:论坛游民
帖 子:50
专家分:30
注 册:2009-4-27
收藏
得分:0 
再顶一下。  高手在哪里。。。。
2009-10-31 22:30
xuyao18
Rank: 2
等 级:论坛游民
帖 子:50
专家分:30
注 册:2009-4-27
收藏
得分:0 
补充提问:
这个就是题目,基本上就是输入N个(X,Y),然后把所有的X相加,所有的Y 相加,在除以N,得出重心。
There are many secret openings in the floor which are covered by a big heavy stone. When the stone is lifted up, a special mechanism detects this and activates poisoned arrows that are shot near the opening. The only possibility is to lift the stone very slowly and carefully. The ACM team must connect a rope to the stone and then lift it using a pulley. Moreover, the stone must be lifted all at once; no side can rise before another. So it is very important to find the centre of gravity and connect the rope exactly to that point. The stone has a polygonal shape and its height is the same throughout the whole polygonal area. Your task is to find the centre of gravity for the given polygon.

Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer N (3 <= N <= 1000000) indicating the number of points that form the polygon. This is followed by N lines, each containing two integers Xi and Yi (|Xi|, |Yi| <= 20000). These numbers are the coordinates of the i-th point. When we connect the points in the given order, we get a polygon. You may assume that the edges never touch each other (except the neighboring ones) and that they never cross. The area of the polygon is never zero, i.e. it cannot collapse into a single line.

Output
Print exactly one line for each test case. The line should contain exactly two numbers separated by one space. These numbers are the coordinates of the centre of gravity. Round the coordinates to the nearest number with exactly two digits after the decimal point (0.005 rounds up to 0.01). Note that the centre of gravity may be outside the polygon, if its shape is not convex. If there is such a case in the input data, print the centre anyway.

Sample Input
2
4
5 0
0 5
-5 0
0 -5
4
1 1
11 1
11 11
1 11



然后我写的这个程序


#include <stdio.h>
void main ()
{
    int x,n,m,i,j,a[100][2];
    double t1,t2;
    scanf("%d",&x);
    for(n=0;n<x;n++)
    {
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        scanf("%d",&a[i][0]);
        scanf("%d",&a[i][1]);
    }
    for(j=1;j<m;j++)
    {
        a[0][0]+=a[j][0];
        a[0][1]+=a[j][1];
    }
    t1=double(a[0][0]);
    t2=double(a[0][1]);
    t1=t1/m;
    t2=t2/m;
    t1=t1*1000+0.5;
    t1=t1/1000.0;
    t2=t2*1000+0.5;
    t2=t2/1000.0;

    printf("%.2f\n",t1);
    printf("%.2f\n",t2);
    }
}


 我实在是想不出来有什么错误的了,但是一直无法AC,在小数点的处理上,可能还有点生疏,刚从百度上查的。。呵呵
希望各位大大指点。
2009-10-31 23:17
xuyao18
Rank: 2
等 级:论坛游民
帖 子:50
专家分:30
注 册:2009-4-27
收藏
得分:0 
冰天雪地,前翻滚三百六十度,跪求高手指教
2009-11-01 12:18
ljt0000mf
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:104
专家分:157
注 册:2009-7-4
收藏
得分:5 
你这个程序,有个明显的错误。
 

这里,我就不说了
2009-11-01 12:22
Kid_X
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:216
专家分:515
注 册:2007-10-8
收藏
得分:5 
    t1=double(a[0][0]);
    t2=double(a[0][1]);
这两句强制类型转换错了,应改成:
    t1=(double)a[0][0];
    t2=(double)a[0][1];

至于逻辑设否正确就不知道了。
2009-11-01 12:22
xuyao18
Rank: 2
等 级:论坛游民
帖 子:50
专家分:30
注 册:2009-4-27
收藏
得分:0 
回复 7楼 Kid_X
我也试过不用强制类型转换,就是直接把数组定义成double类型,但是貌似错误还是存在。
凸多边形求重心,这个应该不会错吧。。
2009-11-01 12:25
xuyao18
Rank: 2
等 级:论坛游民
帖 子:50
专家分:30
注 册:2009-4-27
收藏
得分:0 
回复 6楼 ljt0000mf
=。=#。。。。  大哥,您别耍我好么。。。?  
2009-11-01 12:25
Kid_X
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:216
专家分:515
注 册:2007-10-8
收藏
得分:0 
不好意思,前面没搞清楚。
首先提个建议,推荐使用结构体。
    if(m=2)    // 判断相等用 ==
        for(i=0;i<1;i++)
    {
        flag=cross(a[i][0],a[i][1],a[i][2],a[i][3],a[i+1][0],a[i+1][1],a[i+1][2],a[i+1][3]);
        sum+=flag;
    }
2009-11-01 12:45
快速回复:求助C语言编程的几何题目~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035039 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved