You can Solve a Geometry Problem too(计算多个线段的交点个数)

You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8647    Accepted Submission(s): 4210


Problem Description
Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.

Note:
You can assume that two segments would not intersect at more than one point. 
 

Input
Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending. 
A test case starting with 0 terminates the input and this test case is not to be processed.
 

Output
For each case, print the number of intersections, and one line one case.
 

Sample Input
  
  
2 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.00 3 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.000 0.00 0.00 1.00 0.00 0
 

Sample Output
  
  
1 3
 

Author
lcy
 

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef struct point
{
    float x;
    float y;
}Point;
//判断直线AB是否与线段CD相交
bool lineIntersectSide(Point A, Point B, Point C, Point D)
{
    // A(x1, y1), B(x2, y2)的直线方程为:
    // f(x, y) =  (y - y1) * (x1 - x2) - (x - x1) * (y1 - y2) = 0

    float fC = (C.y - A.y) * (A.x - B.x) - (C.x - A.x) * (A.y - B.y);
    float fD = (D.y - A.y) * (A.x - B.x) - (D.x - A.x) * (A.y - B.y);

    if(fC * fD > 0)
        return false;

    return true;
}
bool sideIntersectSide(Point A, Point B, Point C, Point D)
{
    if(!lineIntersectSide(A, B, C, D))
        return false;
    if(!lineIntersectSide(C, D, A, B))
        return false;
    return true;
}
int main()
{
    int n,num,i,j;
    Point p[105][2];
    while(cin>>n,n)
    {
        num=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<2;j++)
            {
                cin>>p[i][j].x>>p[i][j].y;
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(sideIntersectSide(p[i][0],p[i][1],p[j][0],p[j][1]))
                {
                    num++;
                }
            }
        }
        cout<<num<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您在代码中遇到的错误信息 "SOLVE requires a non-empty initial point structure to solve a nonlinear problem." 表示您使用的求解器 `solve` 需要提供一个非空的初始点结构体来解决非线性问题。 这个错误通常是由于没有正确设置初始点结构体 `x0` 导致的。您需要确保您的初始点结构体是非空的,并且包含了优化问题中所有变量的初始值。 请检查如下几个方面: 1. 确保您已经正确定义了初始点结构体 `x0`,并为其中的字段设置了合适的初始值。您可以使用 `struct` 函数创建一个空的结构体,并使用点运算符 `.` 为每个字段赋值。 2. 确保初始点结构体 `x0` 中包含了优化问题中所有变量的初始值。这些变量的名称应与问题定义中使用的变量名称相匹配。 3. 确保初始点结构体 `x0` 是非空的。您可以使用 `isempty` 函数检查结构体是否为空,如果为空,则需要重新设置初始点。 以下是一个示例代码,演示如何设置一个非空的初始点结构体: ```matlab % 创建一个初始点结构体 x0 = struct(); % 设置结构体的字段值为优化变量的初始值 x0.field1 = initial_value1; x0.field2 = initial_value2; % ... % 检查初始点结构体是否为空 if isempty(x0) error('初始点结构体为空,请设置合适的初始值。'); end % 将初始点结构体传递给 solve 函数进行求解 [solution, objectiveValue, reasonSolverStopped] = solve(problem, x0); ``` 请根据您的优化问题和具体需求,设置正确的初始点结构体,并确保它是非空的。 希望这可以解决您的问题!如果您有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值