Project1

标题: [MATH]没错还是数学问题,不过这可以算计算机图形学= = [打印本页]

作者: yangff    时间: 2011-6-10 16:36
标题: [MATH]没错还是数学问题,不过这可以算计算机图形学= =
本帖最后由 yangff 于 2011-6-10 16:38 编辑

没错……数学问题,判定俩矩形(如果是圆形和矩形就更好了)香蕉……我是写过多边形的没错……但是问题在于
判定相交的同时,还需要返回相交的是哪条边
画张图就比较明白了
最近脑子实在不清楚……
写个代码写成这个鸟样……

最后请将他们封装成类……
  1. class pointF
  2. attr_accessor:x
  3. attr_accessor:y
  4.   def initialize(x,y)
  5.     @x=x
  6.     @y=y
  7.   end
  8. end
  9. class Rectangle
  10. attr_accessor:left_up
  11. attr_accessor:right_down
  12.   def initialize(p1,p2)
  13.     left_up=p1
  14.     right_down=p2
  15.   end
  16.   def &(v)
  17.     #请完成本段代码

  18.   end
  19. end
复制代码

Thx
注:不用考虑交成十字架的,或者直接返回四边相交= =
最后吐槽一下……系统吃了我100EXpdsu_plus_rewardpost_czw
作者: 沙漠点灰    时间: 2011-6-10 19:36
lz要求有点高,貌似某些只能算是相切吧...?送上一个手工枚举版,lz的要求没有完成...
RGSS给了个Rect类,直接修改~~
  1. #==============================================================================
  2. # ■ Rect
  3. #------------------------------------------------------------------------------
  4. #  矩形类 吐槽无力~
  5. #==============================================================================

  6. class Rect
  7.   QWERTY = [2, 4, 6, 8]
  8.   #--------------------------------------------------------------------------
  9.   # ● 香蕉判定
  10.   #--------------------------------------------------------------------------
  11.   def &(rect)
  12.     raise(RGSSError,"丫的不给力啊!") unless rect.is_a?(Rect)
  13.     lu = range(rect.x, rect.y)
  14.     rd = range(rect.x+rect.width, rect.y+rect.height)
  15.     return false if lu == rd
  16.     unless [lu,rd].include?(5)
  17.       return 0 if ([lu,rd] & QWERTY).size == 2
  18.       return false if((lu-1)/3==(rd-1)/3)or(lu-1)%3==(rd-1)%3 or(lu == 7 and rd == 3)
  19.       case lu
  20.       when 7
  21.         return rd == 2 ? 14 : 18
  22.       when 8
  23.         return rd == 6 ?  9 : 16
  24.       when 4
  25.         return rd == 2 ?  1 : 12
  26.       end
  27.     else
  28.       return lu == 5 ? rd : lu
  29.     end
  30.     raise(RGSSError,"未判断错误...#{[lu, rd]}")
  31.   end
  32.   #--------------------------------------------------------------------------
  33.   # ● 范围判定
  34.   #--------------------------------------------------------------------------
  35.   def range(x, y, site=false)
  36.     unless site
  37.       return range(x, y, x>self.x ?(y>self.y ? 3:9):(y>self.y ? 1:7))
  38.     else
  39.       return 7 if site == 7
  40.       site2 =(x>self.x+self.width) ? (y>self.y+self.height ? 3:9):(y>self.y+self.height ? 1:7)
  41.       case site
  42.       when 9
  43.         return site2 == 9 ? 9 : 8
  44.       when 1
  45.         return site2 == 1 ? 1 : 4
  46.       when 3
  47.         return site2 == 1 ? 2 : site2 == 7 ? 5 : site2 == 3 ? 3 : 9
  48.       end
  49.     end
  50.   end
  51. end
复制代码
返回false不香蕉,其它值香蕉,其他值清单:

0:十字香蕉

12346789:对应小键盘12346789位置,lz自己看着办...

12:下大交(2是下小交)

16:右大交(6是右小交)

14:左大交(4是左小交)

18:上大交(8是上小交)

相切的话会bug掉...........
作者: uniquetruth    时间: 2011-6-10 20:45
本帖最后由 uniquetruth 于 2011-6-10 20:46 编辑
  1. class PointF
  2. attr_accessor:x
  3. attr_accessor:y
  4.   def initialize(x,y)
  5.     @x=x
  6.     @y=y
  7.   end
  8. end
  9. class Rectangle
  10. attr_accessor:left_up
  11. attr_accessor:right_down
  12.   def initialize(p1,p2)
  13.     @left_up=p1
  14.     @right_down=p2
  15.   end
  16.   def check(rectA)    #返回此Rectangle哪条边与rectA相交
  17.     #有边重叠的情况
  18.     if rectA.right_down.x == left_up.x
  19.       unless rectA.left_up.y >= right_down.y or rectA.right_down.y <= left_up.x
  20.         return 1  #左边
  21.       end
  22.     end
  23.     if rectA.left_up.x == right_down.x
  24.       unless rectA.left_up.y >= right_down.y or rectA.right_down.y <= left_up.x
  25.         return 2  #右边
  26.       end
  27.     end
  28.     if rectA.right_down.y == left_up.y
  29.       unless rectA.right_down.x <= left_up.x or rectA.left_up.x >= right_down.x
  30.         return 3  #上边
  31.       end
  32.     end
  33.     if rectA.left_up.y == right_down.y
  34.       unless rectA.right_down.x <= left_up.x or rectA.left_up.x >= right_down.x
  35.         return 4  #下边
  36.       end
  37.     end
  38.     #没有边重叠的情况
  39.     if rectA.right_down.x > left_up.x and
  40.       rectA.left_up.x < left_up.x and
  41.       ((rectA.left_up.y < left_up.y and rectA.right_down.y > right_down.y) or
  42.       (rectA.left_up.y > left_up.y and rectA.right_down.y < right_down.y))
  43.       return 1  #左边
  44.     end
  45.     if rectA.left_up.x < right_down.x and
  46.       rectA.right_down.x > right_down.x and
  47.       ((rectA.left_up.y < left_up.y and rectA.right_down.y > right_down.y) or
  48.       (rectA.left_up.y > left_up.y and rectA.right_down.y < right_down.y))
  49.       return 2  #右边
  50.     end
  51.     if rectA.right_down.y > left_up.y and
  52.       rectA.left_up.y < left_up.y and
  53.       ((rectA.left_up.x < left_up.x and rectA.right_down.x > right_down.x) or
  54.       (rectA.left_up.x > left_up.x and rectA.right_down.x < right_down.x))
  55.       return 3  #上边
  56.     end
  57.     if rectA.left_up.y < right_down.y and
  58.       rectA.left_up.y < left_up.y and
  59.       ((rectA.left_up.x < left_up.x and rectA.right_down.x > right_down.x) or
  60.       (rectA.left_up.x > left_up.x and rectA.right_down.x < right_down.x))
  61.       return 4  #下边
  62.     end
  63.     if rectA.right_down.x > left_up.x and
  64.       rectA.left_up.x < left_up.x and
  65.       rectA.right_down.x < right_down.x and
  66.       rectA.left_up.y < left_up.y and
  67.       rectA.right_down.y > left_up.y and
  68.       rectA.right_down.y < right_down.y
  69.       return 5  #左上
  70.     end
  71.     if rectA.left_up.x > left_up.x and
  72.       rectA.left_up.x < right_down.x and
  73.       rectA.right_down.x > right_down.x and
  74.       rectA.left_up.y < left_up.y and
  75.       rectA.right_down.y > left_up.y and
  76.       rectA.right_down.y < right_down.y
  77.       return 6  #右上
  78.     end
  79.     if rectA.right_down.x > left_up.x and
  80.       rectA.left_up.x < left_up.x and
  81.       rectA.right_down.x < right_down.x and
  82.       rectA.left_up.y > left_up.y and
  83.       rectA.left_up.y < right_down.y and
  84.       rectA.right_down.y > right_down.y
  85.       return 7  #左下
  86.     end
  87.     if rectA.left_up.x > left_up.x and
  88.       rectA.left_up.x < right_down.x and
  89.       rectA.right_down.x > right_down.x and
  90.       rectA.left_up.y > left_up.y and
  91.       rectA.left_up.y < right_down.y and
  92.       rectA.right_down.y > right_down.y
  93.       return 8 #右下
  94.     end
  95.     return 0  #不相交
  96.   end
  97. end
复制代码
LZ你用点心冷静下来仔细想想就枚举出来了啊
随便测试了几个整数,应该没问题
作者: yangff    时间: 2011-6-10 22:18
uniquetruth 发表于 2011-6-10 20:45
LZ你用点心冷静下来仔细想想就枚举出来了啊
随便测试了几个整数,应该没问题 ...

不行这这个还是有问题
[(504,6)(532,24)].check( [(0,0)(614,5)])
他会返回4,也就是下边相交……




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1