怎样判断鼠标点击在平行四边形里
我画一个平行四边形,当鼠标点击时,怎么判断鼠标时点击在图形里还是图形外? --------------------编程问答-------------------- 纯粹的数学问题, --------------------编程问答-------------------- 按照像素判断或者按照平行四边形的外边判断 --------------------编程问答-------------------- 我知道平行四边形的四个角顶点的坐标,想简单点解决这个问题。能有什么好办法 --------------------编程问答-------------------- 方法很多,其中一种:设要判断的点为P,以P点为始点作一射线(为简化计算,可水平向左作),然后计算射线与平形四边形的边的交点个数,若为奇数,在内,反之在外。
再处理一下所作射线与边重合的特殊情况即可;
更多信息,百度"判断点在多边形内"
--------------------编程问答-------------------- 谢谢你,能不能具体给点提示,我是第一次用vb编程序,很多都不懂 --------------------编程问答-------------------- 左上角坐标:(x1,y1)
右上角坐标:(x2,y2)
左下角坐标:(x3,y3)
右下角坐标:(x4,y4)
欲判断的点的坐标:(x,y)
if (x>x1) and (x>x3) and (x<x2) and (x<x4) and (y>y1) and (y>y2) and (y<y3) and (y<y4) then
debug.print "点落在平行四边形中"
endif
--------------------编程问答-------------------- 用PtInRegion函数。当然得先生成平形四边形的区域
Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long
--------------------编程问答--------------------
数学要搞好,最起码解析几何要知道点儿,我也不是学VB的!
7楼给了你个API也能用,这里给你前面算法的VB实现,几行核心代码:
Option Explicit
'定义点
Private Type Point
x As Single
y As Single
End Type
'定义四边形,但这个算法对任意多边形都适用
Private Type Quadrilateral
p(1 To 4) As Point '四边形的四个顶点
End Type
Dim Qdl As Quadrilateral
'核心在这儿,测试点Pt是否在四边形Qdl中
Private Function PtInQuadrilateral(Qdl As Quadrilateral, pt As Point) As Boolean
Dim i As Integer
Dim c As Integer '交点数
Dim x As Single '射线与四边形各边所在直线交点的横坐标
c = 0
For i = 1 To 4
Dim p1 As Point, p2 As Point
p1 = Qdl.p(i): p2 = Qdl.p(i Mod 4 + 1)
If p2.y - p1.y <> 0 Then '如果不和射线平行
'计算射线与四边形各边所在直线交点的坐标
x = (p2.x - p1.x) / (p2.y - p1.y) * (pt.y - p1.y) + p1.x
If x < pt.x And pt.y > min(p1.y, p2.y) And pt.y < max(p1.y, p2.y) Then '如果射线与边相交
c = c + 1
End If
End If
Next
PtInQuadrilateral = c Mod 2 <> 0 '交点数不为偶数,则点在四边形中
End Function
'辅助,取最大值
Private Function max(a As Single, b As Single) As Single
max = IIf(a > b, a, b)
End Function
'辅助,取最小值
Private Function min(a As Single, b As Single) As Single
min = IIf(a > b, b, a)
End Function
'以下是测试代码
'生成用来测试的四边形,不一定是平形四边形,但一样适用
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim i As Integer, j As Long
Randomize
Me.Cls
For i = 1 To 3
Qdl.p(i).x = 8000 * Rnd(1)
Qdl.p(i).y = 8000 * Rnd(1)
Next
'画出四边形
For i = 1 To 4
Me.Line (Qdl.p(i).x, Qdl.p(i).y)-(Qdl.p(i Mod 4 + 1).x, Qdl.p(i Mod 4 + 1).y)
Next
End Sub
'先在form上添加个label1,移动鼠标试试
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim pt As Point
pt.x = x
pt.y = y
If PtInQuadrilateral(Qdl, pt) Then
Label1.Caption = "点(" & pt.x & "," & pt.y & ")在四边形中"
Else
Label1.Caption = "点(" & pt.x & "," & pt.y & ")不在四边形中"
End If
End Sub
最后还是一句,数学很重要 --------------------编程问答--------------------
我第一次见到这个函数,还不会用,谢谢你的指点,我正在努力寻找解决办法 --------------------编程问答-------------------- 纯数学问题. --------------------编程问答--------------------
世界大多数问题就是数学问题。数学厉害的人物哪一个不是大家! --------------------编程问答-------------------- 还没解决?? --------------------编程问答--------------------
我在研究你的程序呢,比较难。ptinregion函数,我不明白 --------------------编程问答--------------------
重点在PtInQuadrilateral函数中,其它过程是用来测试的,这个函数就相当于PtInRegion,不过PtInRegion要强大得多 --------------------编程问答-------------------- 几何运算问题我就不多说了,数学不好肯定问题很多,这种东西还是要多花时间去学习的。
不过我可以提供个简单的思路解决这种问题,看看下面我的博客:
http://blog.csdn.net/supermanking/article/details/5538059 --------------------编程问答--------------------
Option Explicit--------------------编程问答--------------------
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Const ALTERNATE = 1
Private Declare Function Polygon Lib "gdi32" (ByVal hDc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private mudtMyPoint() As POINTAPI
Private Sub Form_Load()
Me.ScaleMode = vbPixels
'定义多边形
ReDim mudtMyPoint(3)
mudtMyPoint(0).X = 20: mudtMyPoint(0).Y = 20
mudtMyPoint(1).X = 200: mudtMyPoint(1).Y = 20
mudtMyPoint(2).X = 260: mudtMyPoint(2).Y = 200
mudtMyPoint(3).X = 80: mudtMyPoint(3).Y = 200
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.Cls
'绘制多边形
Polygon Me.hDc, mudtMyPoint(0), 4
'生成区域
Dim Rgn As Long
Rgn = CreatePolygonRgn(mudtMyPoint(0), 4, ALTERNATE)
If Rgn <> 0 Then
If PtInRegion(Rgn, X, Y) > 0 Then
MsgBox "在多边形内", vbInformation, "提示"
Else
MsgBox "在多边形外", vbInformation, "提示"
End If
DeleteObject Rgn
Else
MsgBox "错误", vbInformation
End If
End Sub
谢谢,我有个小问题,咨询一下,四边形是变量怎么处理到这个程序中 --------------------编程问答-------------------- 四个顶点是根据鼠标移动而画出来的,这个我就不知道怎么处理到你的程序中
补充:VB , 基础类