![]() |
|
#1
|
|||
|
|||
![]()
Hi Folks !
Now i got a Book VB for dummies and wrote a first small makro: Sub neue1() Dim size1, size2, size3, size4, color1, color2, color3, color4 Randomize size1 = Int((20 * Rnd) + 1) size2 = Int((20 * Rnd) + 1) size3 = Int((20 * Rnd) + 1) size4 = Int((20 * Rnd) + 1) color1 = Int((100 * Rnd) + 1) color2 = Int((100 * Rnd) + 1) color3 = Int((100 * Rnd) + 1) color4 = Int((20 * Rnd) + 1) Dim s5 As Shape Set s5 = ActiveLayer.CreateRectangle(size1, size2, size3, size4, 0, 0, 0, 0) s5.Fill.UniformColor.CMYKAssign color1, color2, color3, color4 End Sub It creates a rectangle with random size and color. It works, but sometimes the error occures: The sides of the object cannot be 0. What is my Mistake? |
#2
|
||||
|
||||
![]()
Superfreak,
Your problem is the following. You used Rnd function to generate random numbers. It generates numbers in range between 0 and 1 (including zero but never 1). So when you do it like this: Int(20*Rnd + 1) what you actually are getting is integer numbers between 1 and 20. CreateRectangle requires two bounding points (e.g. upper left and lower right corners) of the rectangle to create. If you specify, let's say, (1,2)-(3,4), then your rectangle will be 2" wide (3-1) and 2" high (4-2). However the chances could be that "size1" and "size3" could be equal and/or "size2" and "size4". In this case, either width or height (or both) of the rectangle will be 0, and an error will occur. I'm just curious if you really wanted to use integer numbers for coordinates in the first place. Second, you would probably be better off if you specify one point (e.g. lower left corner) and the size of the rectangle. In this case, it'd be better to use CreateRectangle2 function which does just that. You can rewrite your example as follows: Code:
Sub TestRandom() Dim x As Double, y As Double Dim sx As Double, sy As Double Dim color1 As Long, color2 As Long, color3 As Long, color4 As Long Dim s As Shape Dim n As Long Randomize For n = 1 To 100 x = 20 * Rnd() y = 20 * Rnd() sx = 19.9 * Rnd() + 0.1 sy = 19.9 * Rnd() + 0.1 color1 = CLng(101 * Rnd()) color2 = CLng(101 * Rnd()) color3 = CLng(101 * Rnd()) color4 = CLng(101 * Rnd()) Set s = ActiveLayer.CreateRectangle2(x, y, sx, sy) s.Fill.UniformColor.CMYKAssign color1, color2, color3, color4 Next n End Sub Also the color components are generated between 0 and 100 inclusive. I just had to multiply Rnd by 101 in order to achieve that. Because Rnd generates numbers that are ALWAYS less than 1 (but could be very close), after multiplying by 101 you'll get a maximum of something like 100.999 which will be converted to 100 when assigning it to a Long variable. Your previous formula used to give values between 1 and 100 and never 0. I also put a loop to generate 100 rectangles for test purposes. Feel free to remove it. I hope this helps. |
#3
|
|||
|
|||
![]()
I see... and understand!!!!
Thanks for you enlightment!! I will enhance it some more with a formular. ![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
File Converter script error | CorelDummy | CorelDRAW/Corel DESIGNER VBA | 2 | 25-01-2005 08:38 |
CorelDraw 8 error in DLL file - help !!! cannot open!!! | Anonymous | General | 4 | 31-10-2004 15:52 |
Export filter compile error | ddonnahoe | CorelDRAW/Corel DESIGNER VBA | 1 | 26-01-2004 09:15 |
"Unexpected condition" error message (with code) | s_federici | CorelDRAW/Corel DESIGNER VBA | 10 | 10-07-2003 21:46 |
"Object too complex" error | dcsquare | CorelDRAW/Corel DESIGNER VBA | 0 | 25-05-2003 10:13 |