|
-
May 16th, 2008, 10:10 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Finding all X/Y points between two given points
Hi,
I have two X/Y points on my form. I need to find every X/Y between the two as if you were drawing a line. I tried thinking how to do it but I'm having trouble.
I know I need as many steps in between as possible. For example, if X1 and X2 are only one pixel apart and Y1 and Y2 are 20 pixels apart, I need 20 points in between. So I need the most depending on if the X or Y has the biggest difference.
I should know how to do this but its been a long morning!
-
May 16th, 2008, 11:57 AM
#2
Re: Finding all X/Y points between two given points
Ooh, here's something I wrote a little while ago.
It returns an array of points
Code:
Option Explicit
Public Type lngCoord2D
X As Long
Y As Long
End Type
Public Function GetLinePixels(ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As lngCoord2D()
Dim Ln() As lngCoord2D, i As Long, ii As Long
Dim VX As Long, VY As Long, M As Double, C As Double
VX = X2 - X1
VY = Y2 - Y1
If Abs(VX) > Abs(VY) Then
ReDim Ln(Abs(VX))
M = VY / VX
C = Y1 - X1 * M
For i = X1 To X2 Step Sgn(VX)
Ln(ii).X = i
Ln(ii).Y = (i * M + C)
ii = ii + 1
Next i
ElseIf VY = 0 Then
ReDim Ln(0)
Ln(0).X = X1
Ln(0).Y = Y1
Else
ReDim Ln(Abs(VY))
M = VX / VY
C = X1 - Y1 * M
For i = Y1 To Y2 Step Sgn(VY)
Ln(ii).X = (i * M + C)
Ln(ii).Y = i
ii = ii + 1
Next i
End If
GetLinePixels = Ln
End Function
-
May 16th, 2008, 12:23 PM
#3
PowerPoster
Re: Finding all X/Y points between two given points
 Originally Posted by WarrenW
Hi,
I have two X/Y points on my form. I need to find every X/Y between the two as if you were drawing a line. I tried thinking how to do it but I'm having trouble.
I know I need as many steps in between as possible. For example, if X1 and X2 are only one pixel apart and Y1 and Y2 are 20 pixels apart, I need 20 points in between. So I need the most depending on if the X or Y has the biggest difference.
I should know how to do this but its been a long morning!
I think this might give you what you need, but it I didnt really test it much
I really dont understand how you are caculating the number of points though ... shouldnt it just be square root of the sum of the deltas squared?
Code:
Private Sub Command1_Click()
Dim i As Integer, x(1) As Double, y(1) As Double
Dim yInt As Double, slope As Double
Dim tstY As Double
x(0) = 1: x(1) = 200: y(0) = 1: y(1) = 200
slope = (y(1) - y(0)) / (x(1) - x(0))
yInt = y(1) - slope * x(1)
Dim strResults As String
For i = x(0) To x(1)
tstY = slope * i + yInt
If tstY - Fix(tstY) = 0 Then
strResults = strResults & i & "," & tstY & vbCrLf
End If
Next i
MsgBox strResults
End Sub
-
May 16th, 2008, 12:50 PM
#4
Re: Finding all X/Y points between two given points
 Originally Posted by Muddy
I really dont understand how you are caculating the number of points though ... shouldnt it just be square root of the sum of the deltas squared?
Think about it. The number of points will always be the larger of the two vectors (is that the right term? i.e. X2-X1 or Y2-Y1). That code I posted calculates the points from the perspective of the larger vector so that no pixels are ever left out.
-
May 16th, 2008, 01:24 PM
#5
PowerPoster
Re: Finding all X/Y points between two given points
 Originally Posted by Milk
Think about it. The number of points will always be the larger of the two vectors (is that the right term? i.e. X2-X1 or Y2-Y1). That code I posted calculates the points from the perspective of the larger vector so that no pixels are ever left out.
Im just speaking from a math point of view ... Im definitely not a graphics programmer, so there is probably something I am missing ...
IF x and y are screen coordinates I can see where you would have to convert to pixel locations before doing the math, but as far as I know the number of integer coordinate points between two coordinates on a straight line is sqr(deltax^2 + deltay^2) ...
but, I can see how that wouldnt work very well if there are more pixels per inch in one dimension compared to the other ...
-
May 16th, 2008, 01:26 PM
#6
Thread Starter
Frenzied Member
Re: Finding all X/Y points between two given points
Thanks Milk! Can you show me how to call this? Sorry, I keep doing something wrong...
-
May 16th, 2008, 01:38 PM
#7
Re: Finding all X/Y points between two given points
Muddy, I can see where you are coming from, that will give you the distance between the two points not the number of pixels between two points.
WarrenX,
Code:
Dim Pts() as lngCoord2d,i as long
Pts = GetLinePixels(91,23,45,67)
'Job done, now just to show we have them...
For i= 0 to ubound(pts)
me.pset (pts(i).x,pts(i).y)
next i
-
May 16th, 2008, 01:52 PM
#8
Thread Starter
Frenzied Member
Re: Finding all X/Y points between two given points
Works great!!! THANK YOU!!!!!!!!!!!!!!!!!!
Warren
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|