|
-
Aug 6th, 2007, 04:45 PM
#1
Thread Starter
Hyperactive Member
Picturebox and Lines
Hello.
I am using a picturebox and I am drawing a BLUE line in it (always different X-Y/X-Y coords).
Once the blue line is drawn, I do a loop of all pixels from that picturebox, searching for BLUE pixels. When I hit a blue pixel, I can compare this to ANOTHER picturebox (using same X,Y coords - also using the same picture) to see if the blue line is overlapping red, black, white or yellow pixels and so on.
This works good. My only problem is that looping the entire picturebox can take quite a while on my P4-2.8ghz. (note here that I am doing several loops)
In clear, it would be wonderful if there was a quick way to know WHICH X,Y pixels were changed to BLUE after I ran the picPictureBox.Line command.
Any tips?
Thank you!
-
Aug 6th, 2007, 04:56 PM
#2
Hyperactive Member
Re: Picturebox and Lines
You'd be much faster using a safearray with CopyMemory() than using Point().. API graphics/memory functions are waay faster
-
Aug 6th, 2007, 05:07 PM
#3
Thread Starter
Hyperactive Member
Re: Picturebox and Lines
Hi. Thanks for the quick reply.
I've played with arrays before... but safearray? used with CopyMemory()? Guess I'll have to search the forum because I didn't know about CopyMemory. If you have any examples on this, feel free to post, but I guess I'll find on the forums.
I'd like to check the logic with you tho. I guess I would have to:
1- Copy picOriginal to array1
2- Draw blue line on picBlueLine
3- Copy picBlueLine to array2
4- Search array2 for blue pixels and compare X,Y coords to array1
...and loop steps 2,3,4 (because I am comparing several lines)
Do I have a fairly good logic on this? If so, do you think that step3 (copying picBlueLine to array2), after every new blue line, will be quicker than just comparing with .Point? If so - I'm glad, I'll try out. It'll have to copy a picturebox (300 pixels / 70 pixels) to array2 about 500 times, only to pinpoint the location of a newly added blue line.
Hey thanks a lot for your input, 'appreciate it.
-
Aug 6th, 2007, 05:13 PM
#4
Hyperactive Member
Re: Picturebox and Lines
There is a demo on the internet for "calphadibsection.cls" that demonstrates it pretty well..
-
Aug 6th, 2007, 06:00 PM
#5
Re: Picturebox and Lines
DibSections and SafeArray structures are defiantly the keywords to be searching for. Steve McMahon's code of vbaccelerator is probobly more than you need. The beauty of using the Safearray structure with a DibSection is that you don't have to copy the image at all, you merely copy pointer to the data and hey presto, you have the image in an array.
One more thing, do you have to draw lines at all? (and why blue?) A line can be defined by X=M*Y + C. Can you not use that (or similar) to define the pixels of a line in array form?
-
Aug 7th, 2007, 02:56 PM
#6
Thread Starter
Hyperactive Member
Re: Picturebox and Lines
Milk, thanks for making things clear. I'll gather some info on SafeArrays, soon.
And no, I don't have to draw lines at all. That's just the only way I thought of doing things: I draw the line so I can then check the X,Y coords it has used, to THEN compare it to an original image.
Why blue? no reason. Could have been any color. Just that I developped my app to check for blue pixels once the line is drawn.
Using X=M*Y + C to get X,Y coords of a line seems like a pretty good idea! Are you positively sure it'll return the exact same pixels as if I used the ".line" command over a picturebox? If so, I'm thrilled: I won't have to draw those blue lines AT ALL.
The only thing I'm not sure about is what M and C are in the equation X=M*Y + C. That must be pretty basic, but I havn't touched that in a while. Guess I'll have to ask my math-studying-girlfriend... Hope she knows how to sort this out.
What I'll need is all pixels used to draw a line between, let's say, (33,0) and (101,78).
Thanks a lot - very helpful input here.
-
Aug 7th, 2007, 03:05 PM
#7
Frenzied Member
Re: Picturebox and Lines
 Originally Posted by Milk
....A line can be defined by X=M*Y + C. Can you not use that (or similar) to define the pixels of a line in array form?
this should be y = mx+C right?
-
Aug 7th, 2007, 08:44 PM
#8
Re: Picturebox and Lines
Actually Y=MX+C is the same as X=MY+C only that M and C will be different constants. M is just the multiplier and C the offset, any straight line can be defined by this.
Y = 2 * X + 3 is the same as X = 0.5 * y - 1.5
If you were determining the pixels you would go with the later because the multiplier is less than (or equal to) one. Something like...
Code:
Private Type lngCoord2D
x As Long
y As Long
End Type
.....
Dim y As Long
Dim SY As Long, EY As Long
Dim MyLinePixels() As lngCoord2D
SY = -13: EY = 22
ReDim MyLinePixels(SY To EY)
For y = SY To EY
MyLinePixels(y).y = y
MyLinePixels(y).x = Int(0.5 * y - 1.5)
Next y
Edit: I've a vague memory of an API which might even do this for you.
Last edited by Milk; Aug 7th, 2007 at 09:11 PM.
-
Aug 7th, 2007, 09:47 PM
#9
Frenzied Member
Re: Picturebox and Lines
 Originally Posted by Milk
Actually Y=MX+C is the same as X=MY+C only that M and C will be different constants.
wow the math I learned never said anything like this. the formula for straight line is Y = mX + c
m = Slope or Tangent
c = intercept
 Originally Posted by Milk
M is just the multiplier and C the offset, any straight line can be defined by this.
Y = 2 * X + 3 is the same as X = 0.5 * y - 1.5
well this is right. you are saying two cntradicting things.
in the latter Y = 2 * X + 3 is in the Y = mX + C format. NOT X=mY+ C.
it could be given as X = (Y -C)/m, which is X = 0.5 * y - 1.5

see THIS article
-
Aug 8th, 2007, 05:25 AM
#10
Re: Picturebox and Lines
ZeeZee, I'm not saying contradicting things because I state that M and C will be different constants. I was not saying I can arrange one in to the other. I was trying to make the point that they are both valid formula's as would be X=Y/D-C and Y=X/D-C, I could use any one to get a valid result. In this case I'm flipping the formula according to the gradiant in order not to miss any pixels.
Code:
'Module code
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
'Form Code
Private Sub Form_Load()
Dim Ln() As lngCoord2D, i As Long
Show
ScaleMode = vbPixels
Ln() = GetLinePixels(33, 0, 101, 78)
For i = 0 To UBound(Ln)
PSet (Ln(i).X, Ln(i).Y)
Next i
End Sub
-
Aug 8th, 2007, 05:34 AM
#11
Re: Picturebox and Lines
 Originally Posted by Milk
ZeeZee, I'm not saying contradicting things because I state that M and C will be different constants.
Nitpick: There are no constants in the formula for a straight line.
-
Aug 8th, 2007, 06:10 AM
#12
Re: Picturebox and Lines
Not constant as in CONST but constant as in they are of a fixed value for any given straight line.
-
Aug 8th, 2007, 07:33 AM
#13
Frenzied Member
Re: Picturebox and Lines
well Milk, I dont know what you are saying. I m really confused.
the equation for a straight line would be Y = mX + C or X = (Y-C)/m
to find m, we can use m = (Y1-Y2)/(X1-X2), knowing two points in the straight line. then we can find out C using the known values of X,Y and m. This is what I learned in GEomatry and GRphics progrmaing. when we get the equation of the Straight Line, we can use it to find any other point in the line. (then your argument of Knowing Constatns for a GIVEN line, is met)
Anyway, that's what I know. If your code helps Kras then I m happy.
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
|