美文网首页
一份来自92年的代码的学习

一份来自92年的代码的学习

作者: 寽虎非虫003 | 来源:发表于2021-11-29 23:14 被阅读0次

零、代码来源

Point in Polygon Strategies的网格法一节。

一、源代码

原作者大名已经在源代码中。

/* Faster Line Segment Intersection   */
/* Franklin Antonio                   */

/* return values */
#define DONT_INTERSECT 0
#define DO_INTERSECT   1
#define PARALLEL       2


/* The SAME_SIGNS macro assumes arithmetic where the exclusive-or    */
/* operation will work on sign bits.  This works for twos-complement,*/
/* and most other machine arithmetic.                                */
#define SAME_SIGNS( a, b ) \
    (((long) ((unsigned long) a ^ (unsigned long) b)) >= 0 )


/* The use of some short working variables allows this code to run   */
/* faster on 16-bit computers, but is not essential.  It should not  */
/* affect operation on 32-bit computers.  The short working variables*/
/* to not restrict the range of valid input values, as these were    */
/* constrained in any case, due to algorithm restrictions.           */


int lines_intersect(x1,y1,x2,y2,x3,y3,x4,y4,x,y) 
long x1,y1,x2,y2,x3,y3,x4,y4,*x,*y;
{

long Ax,Bx,Cx,Ay,By,Cy,d,e,f,num,offset;
short x1lo,x1hi,y1lo,y1hi;

Ax = x2-x1;
Bx = x3-x4;

if(Ax<0) {                      /* X bound box test*/
  x1lo=(short)x2; x1hi=(short)x1;
  } else {
  x1hi=(short)x2; x1lo=(short)x1;
  }
if(Bx>0) {
  if(x1hi < (short)x4 || (short)x3 < x1lo) return DONT_INTERSECT;
  } else {
  if(x1hi < (short)x3 || (short)x4 < x1lo) return DONT_INTERSECT;
  }

Ay = y2-y1;
By = y3-y4;

if(Ay<0) {                      /* Y bound box test*/
  y1lo=(short)y2; y1hi=(short)y1;
  } else {
  y1hi=(short)y2; y1lo=(short)y1;
  }
if(By>0) {
  if(y1hi < (short)y4 || (short)y3 < y1lo) return DONT_INTERSECT;
  } else {
  if(y1hi < (short)y3 || (short)y4 < y1lo) return DONT_INTERSECT;
  }


Cx = x1-x3;
Cy = y1-y3;

f = Ay*Bx - Ax*By;                  /* both denominator*/

if(f==0) return PARALLEL;

d = By*Cx - Bx*Cy;                  /* alpha numerator*/
if(f>0) {                       /* alpha tests*/
  if(d<0 || d>f) return DONT_INTERSECT;
  } else {
  if(d>0 || d<f) return DONT_INTERSECT;
  }

e = Ax*Cy - Ay*Cx;                  /* beta numerator*/
if(f>0) {                       /* beta tests*/
  if(e<0 || e>f) return DONT_INTERSECT;
  } else {
  if(e>0 || e<f) return DONT_INTERSECT;
  }

/*compute intersection coordinates*/

num = d*Ax;                     /* numerator */
offset = SAME_SIGNS(num,f) ? f/2 : -f/2;        /* round direction*/
*x = x1 + (num+offset) / f;             /* intersection x */

num = d*Ay;
offset = SAME_SIGNS(num,f) ? f/2 : -f/2;
*y = y1 + (num+offset) / f;             /* intersection y */

return DO_INTERSECT;
}

二、尝试理解

/* Faster Line Segment Intersection   */
/* 更快的线段相交   */
/* Franklin Antonio                   */

/* return values */
/* 返回值*/
#define DONT_INTERSECT 0
#define DO_INTERSECT   1
#define PARALLEL       2


/* The SAME_SIGNS macro assumes arithmetic where the exclusive-or    */
/* operation will work on sign bits.  This works for twos-complement,*/
/* and most other machine arithmetic.                                */
/*SAME_SIGNS宏假定算术运算,其中异或操作将处理符号位。这适用于2补码和大多数其他机器算术。*/
/*这操作感觉有一点厉害*/
#define SAME_SIGNS( a, b ) \
    (((long) ((unsigned long) a ^ (unsigned long) b)) >= 0 )


/* The use of some short working variables allows this code to run   */
/* faster on 16-bit computers, but is not essential.  It should not  */
/* affect operation on 32-bit computers.  The short working variables*/
/* to not restrict the range of valid input values, as these were    */
/* constrained in any case, due to algorithm restrictions.           */
/*使用一些短的工作变量可以让这段代码在16位计算机上运行得更快,但这并不是必需的。它不应该影响32位计算机上的操作。短工作变量不限制有效输入值的范围,因为这些值在任何情况下都受到算法限制。*/


int lines_intersect(x1,y1,x2,y2,x3,y3,x4,y4,x,y) 
long x1,y1,x2,y2,x3,y3,x4,y4,*x,*y;
{

long Ax,Bx,Cx,Ay,By,Cy,d,e,f,num,offset;
short x1lo,x1hi,y1lo,y1hi;

/*这儿开始大概在快排*/
Ax = x2-x1;
Bx = x3-x4;

if(Ax<0) {                      /* X bound box test*/
  x1lo=(short)x2; x1hi=(short)x1;
  } else {
  x1hi=(short)x2; x1lo=(short)x1;
  }
if(Bx>0) {
  if(x1hi < (short)x4 || (short)x3 < x1lo) return DONT_INTERSECT;
  } else {
  if(x1hi < (short)x3 || (short)x4 < x1lo) return DONT_INTERSECT;
  }

Ay = y2-y1;
By = y3-y4;

if(Ay<0) {                      /* Y bound box test*/
  y1lo=(short)y2; y1hi=(short)y1;
  } else {
  y1hi=(short)y2; y1lo=(short)y1;
  }
if(By>0) {
  if(y1hi < (short)y4 || (short)y3 < y1lo) return DONT_INTERSECT;
  } else {
  if(y1hi < (short)y3 || (short)y4 < y1lo) return DONT_INTERSECT;
  }

/*这儿开始大概在做跨立实验*/
Cx = x1-x3;
Cy = y1-y3;

f = Ay*Bx - Ax*By;                  /* both denominator*/

//叉积为零则两条线段平行
if(f==0) return PARALLEL;

d = By*Cx - Bx*Cy;                  /* alpha numerator*/
if(f>0) {                       /* alpha tests*/
  if(d<0 || d>f) return DONT_INTERSECT;
  } else {
  if(d>0 || d<f) return DONT_INTERSECT;
  }

e = Ax*Cy - Ay*Cx;                  /* beta numerator*/
if(f>0) {                       /* beta tests*/
  if(e<0 || e>f) return DONT_INTERSECT;
  } else {
  if(e>0 || e<f) return DONT_INTERSECT;
  }

/*compute intersection coordinates*/
/*计算交点坐标*/
/*这下面的都没有懂*/
/*总算看懂了,计算alpha和beta的公式是以同底不同面积的三角形面积之比来算比例,f/2是用来取整的,不是用来参与计算的*/

num = d*Ax;                     /* numerator */
offset = SAME_SIGNS(num,f) ? f/2 : -f/2;        /* round direction*/
*x = x1 + (num+offset) / f;             /* intersection x */

num = d*Ay;
offset = SAME_SIGNS(num,f) ? f/2 : -f/2;
*y = y1 + (num+offset) / f;             /* intersection y */

return DO_INTERSECT;
}



相关文章

  • 一份来自92年的代码的学习

    零、代码来源 Point in Polygon Strategies[http://erich.realtimer...

  • C语言实现数字走马灯

    代码来自老师,仅做学习使用。

  • Sublime3格式化html/css/js代码和json文件。

    一、前言 当你在前端编程学习探索中,遇到一份看似很好的代码,想copy下来,调试预览并学习。但是源代码对象可能...

  • 来自代码的反馈

    有时, 编程感觉就像在泥泞中爬山。没走一步都需要付出巨大的努力,走上三步就会后退两步。 但是,作为一名专业人士,你...

  • 代码整理

    代码整理 这几天整理2019年来自己写的代码,学习的代码。 在idea上,创建了好idea2019,idea202...

  • 新晋开发?你昨天就应该学习Git啦

    我对新学习开发人员的建议是:每天学习Git和并在Github发布代码。 我每天都会收到来自刚刚开始学习如何编写代码...

  • Ruby 线程基础

    本文是学习笔记,学习过程中主要阅读和参考了以下资料,记录的代码片断也来自以下链接。部分代码稍作了修改: Worki...

  • 利用Python自动批量登录QQ号!马总会聘用我吗?

    Python自动登录QQ 这是一份来自网络的代码,经过了我的修改和验证。 首先我的运行环境是Python3.7,需...

  • 20170305多想一步

    本周我们学习了并联与串联,将工作与个人提升并联,就可赚两倍薪水,一份来自工资,另一份来自个人提升,所以,为自己工作...

  • Cart 回归树

    代码来自《机器学习实战一书》;代码已经由github的wzy6642整理成python3版本:https://gi...

网友评论

      本文标题:一份来自92年的代码的学习

      本文链接:https://www.haomeiwen.com/subject/vgnlxrtx.html