Poj 1269

Intersecting Lines
简而言之就是求两条线的关系,平行?重合?相交?如果相交的话求出交叉点。
方式有很多种,有用向量的,有用斜率的。其实这算是个水题,只不过需要考虑点边界情况。

代码如下:

#include <math.h>
#include <stdio.h>

struct Point {
    double x;
    double y;
    
    bool operator==(const Point &other) const {
        return (x == other.x && y == other.y);
    }
};

/**
 * 假设直线方程为 y = kx + n
 **/
struct Line {
    struct Point a;
    struct Point b;
    
    bool isVertical() {
        return a.x == b.x;
    }
    
    // 这里需要注意直线情况
    double getSlope() {
        return (b.y - a.y)/(b.x - a.x);
    }
    
    double getSuffix() {
        return a.y - getSlope()*a.x;
    }
};

/**
 * 是否平行
 **/
bool isParallel(struct Line line1, struct Line line2) {
    double dxLine1 = line1.a.x - line1.b.x;
    double dyline1 = line1.a.y - line1.b.y;
    
    double dxLine2 = line2.a.x - line2.b.x;
    double dyline2 = line2.a.y - line2.b.y;
    
    if (0 == dxLine1) {
        return (0 == dxLine2);
    }
    
    return (dyline2*dxLine1 == dyline1*dxLine2);
}

/**
 * 是否是同一条直线
 **/
bool isInLine(struct Line line1, struct Line line2) {
    
    // 先计算是否平行,不平行的线一定不会是同一条直线
    if(!isParallel(line1, line2)) {
        return false;
    }
    
    // 平行且过同一个点,则肯定是同一条直线
    if (line1.a == line2.a) {
        return true;
    }
    
    // 否则以 line1.a line2.a 构造第三条线,看是否与 line1 平行
    struct Line lineTmp = {line1.a, line2.a};
    return isParallel(line1, lineTmp);
}

struct Point intersectPoint(struct Line line1, struct Line line2) {
    struct Point point = {0, 0};
    if (line1.isVertical()) {
        point.x = line1.a.x;
        point.y = line2.getSlope()*point.x + line2.getSuffix();
    } else if (line2.isVertical()) {
        point.x = line2.a.x;
        point.y = line1.getSlope()*point.x + line1.getSuffix();
    } else {
        point.x = (line2.getSuffix() - line1.getSuffix())/(line1.getSlope() - line2.getSlope());
        point.y = line1.getSlope()*point.x + line1.getSuffix();
    }
    return point;
}

int main(int argc, const char * argv[]) {
    
    int turn = 0;
    scanf("%d", &turn);
    
    printf("INTERSECTING LINES OUTPUT\n");
    
    for (int i = 0; i < turn; i++) {
        struct Line line1, line2;
        scanf("%lf %lf %lf %lf", &line1.a.x, &line1.a.y, &line1.b.x, &line1.b.y);
        scanf("%lf %lf %lf %lf", &line2.a.x, &line2.a.y, &line2.b.x, &line2.b.y);
        
        if (isInLine(line1, line2)) {
            printf("LINE\n");
            continue;
        }
        
        if (isParallel(line1, line2)) {
            printf("NONE\n");
            continue;
        }
        
        struct Point point = intersectPoint(line1, line2);
        printf("POINT %.2lf %.2lf\n", point.x, point.y);
    }
    printf("END OF OUTPUT");
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Tutorial - 1 (a) 为什么e时代需要生物特征识别我们在信息时代面临诸多问题,如病毒、黑客、计算机盗窃...
    ShellyWhen阅读 7,274评论 0 4
  • 首页 资讯 文章 资源 小组 相亲 登录 注册 首页 最新文章 IT 职场 前端 后端 移动端 数据库 运维 其他...
    Helen_Cat阅读 9,404评论 1 10
  • abbreviation 简写符号;简写 absolute error 绝对误差 absolute value 绝...
    沧海一睹阅读 9,922评论 0 2
  • 听着民谣 世界安静下来 心也沉寂 你会想到很多过往 这很奇特 是因为民谣独特的旋律 还是因为歌手独特的嗓音 或是民...
    野生鲸鱼阅读 910评论 0 0
  • 下雨的夜,格外的静,只有风声和雨声 下雨的夜,格外的美,没有人群也没有车子 在这样的夜,撑一把小紫伞 穿上一双白色...
    是真的废柴阅读 1,599评论 0 1