PDA

Click to See Complete Forum and Search --> : Ploting Points,Math And Recurse


alienheretic
Jan 11th, 2002, 11:31 PM
alienheretic@attbi.com

Ok Here We Go.
I Realy Need Help With This Please.
I Need Code To Cycle Through All 360 Degrees From A Point X,y

The First Time Through
0,45,90,180 Degrees Needs To Be 128 Pixels From Center

The Second Time Through
Should Be half way Between and + or - a Random number
0 and 45,45 and 90,90 and 180, 180 and 360
and the Length should Be Random

The Third Time Through
Should Be A Random Point Between 0 and The first Random Point Between 0 and 45, continueing on through the rest of the spaces

need to be able to go from the first three cycles to say 36 cycles
each time doing the random placement thing

all points need to be in a PTS array Of X and Y Values
so They Can Be Connected Like in The Picture
Please View The Picture It May Make All This easier To Understand

I Will Send $50.00 to
The First Person To Send Me The Working Code With Comments So That I Can Understand How This Was Done.
I Know $50.00 Isnt Much But Its All I Can Afford I HaveKids and A Mortage.
Please Help Thanks
Vincent Foster

MoMad
Jan 12th, 2002, 01:26 AM
huh?

Do you know any trig at all?

2 * PI Radians = 360 Degrees

0, 45, 90, 135, 180 = (for i = 0 to 4) : (i * PI) / 4

There's not alot of work here... if you really sit down and take out a piece of paper and a pen (maybe graphing paper) you should be able to get it in no time.

Now the drawing part is the easiest... use the Line Method or the Circle one!

The formula for a line is:

y = m * x + b

where m is the slope and b is the offset.

also the formula for the degree stuff is:




/|
Hypo- /_|
tnuse / a|
/ | Adjacent
/ |
/ |
-------
Opposite

Sin(a) = Opp / Hyp
Cos(a) = Adj / Hyp
Tan(a) = Opp / Adj


I dont want your money-- I would love to do this (for free) but unfortunately im a bit busy at the mom... this is the kind of stuff that you need to use what u learned back in jr. high... its a "thinking" problem. Also, if you have any specific questions, just post them. Maybe someone will do it for you or even show you have or whatever.

Good Luck,
MoMad

MoMad
Jan 12th, 2002, 01:27 AM
by the way, this is NOT a recursive problem... at least judging the way you explained it.

TiPeRa
Jan 12th, 2002, 07:19 AM
Are you trying to draw a 'radar' graph?

If yes then this will get you started:

Public Type Point
X As Long
Y As Long
End Type
Private Const pi = 3.14159265358979
Public Sub DrawRadarGraph(Center As Point, Radius As Long, ByRef Values() As Long, MaxValue As Long)
Dim Points() As Point
ReDim Points(UBound(Values()))
Form1.Circle (Center.X, Center.Y), Radius
ArrayMax = UBound(Values())
Angle = (360 / ArrayMax) * pi / 180
For i = 0 To ArrayMax - 1
dx = Sin(i * Angle) * Radius
dy = Cos(i * Angle) * Radius
Form1.Line (Center.X, Center.Y)-Step(dx, dy)
Next i
For i = 0 To ArrayMax - 1
dx = Sin(i * Angle) * Values(i) / MaxValue * Radius
dy = Cos(i * Angle) * Values(i) / MaxValue * Radius
Points(i).X = Center.X + dx
Points(i).Y = Center.Y + dy
Next i
For i = 0 To ArrayMax - 2
Form1.Line (Points(i).X, Points(i).Y)-(Points(i + 1).X, Points(i + 1).Y)
Next i
Form1.Line (Points(ArrayMax - 1).X, Points(ArrayMax - 1).Y)-(Points(0).X, Points(0).Y)
End Sub

alienheretic
Jan 12th, 2002, 11:03 AM
http://www.gameprogrammer.com/fractal.html

I Need To Put The 2d Part In A Circle and The Line Format.

Zaei
Jan 12th, 2002, 02:26 PM
TO plot a point on a circle, use this function:

px = r * cos(theta) + cx
py = r * sin(theta) + cy

To convert from degrees to radians (since the trig functions require radians), multiply the degree value by (pi / 180).

To do what you want, simply change the value of r, as it is the distance from the center of the circle. cx and cy is the center of the circle. theta is the radian value. For instance, if you wanted a point plotted at 100 twips from the center, at 45 degrees, with the center at (200, 450), it would look like this:

px = 100 * cos(45 * (3.14159 / 180)) + 200
py = 100 * sin(45 *(3.14159 / 180)) + 450


Z.

beachbum
Jan 12th, 2002, 03:43 PM
Hi Vincent
First of all, i dont want your money :) Buy the kids something nice with it.

The problem is not so difficult in visualising but most of the complexity comes from your requirement to 'remember' each line drawn to get lines for new passes and also in ordering these lines so that you can join the spokes up. I had it all finished last night my time but was too bleary to work out the ordering for the joining of the spokes. NB The code is MUCH smaller without these connections! :p

Anyway, here is your code. You need only to have a command button and a picture box on form1. You can keep clicking the command button to get new graphs.

Regards
Stuart'Type holding details about every spoke
Private Type MyPointType
Length As Long 'Length of spoke
CCWAngle As Long 'Counter clockwise angle limit
'ie the next angle must be between this upper limit
'and the lower limit set by the clockwise angle
Angle As Long 'The actual angle drawn
CWAngle As Long 'Clockwise angle limit
CurX As Single 'Actual X position
CurY As Single 'Actual Y Position
Order As Long 'Position in order for joining lines
End Type
Dim Points() As MyPointType

'Type for details about the order of each spoke so that can connect lines
Private Type MyOrderType
CurX As Single 'Actual X position
CurY As Single 'Actual Y Position
End Type
Dim PointsOrd() As MyOrderType
Dim OrderStart() As Long 'Starting position in order of first line in each pass
Dim OrderJump() As Long 'How much to jump to get next in order for that pass

Private Const pi As Single = 3.14159265 'Constant for Pi
Private Const MaxRadius As Single = 128 'maximum length of spoke in pixels
Private Const LineRand As Long = 41 'Random length shortening of spokes in pixels
Private Const AngleRand As Single = 10 'Random percentage variance for each new angle
Private Const NumPasses As Long = 2 'Number of passes

Dim Loop1 As Long 'Generic loops
Dim Loop2 As Long

Private Sub Form_Load()
Randomize 'Start randomization
ReDim Points(1 To 2 ^ (NumPasses + 2)) 'Create array eg 2 passes = 16 lines
ReDim PointsOrd(1 To 2 ^ (NumPasses + 2)) 'Create array for lines in order
For Loop1 = 1 To 4 'Create starting axis
With Points(Loop1)
.Length = MaxRadius 'Full length lines
.CCWAngle = 90 * (Loop1 - 1) 'CCW limit of quadrant ie 0,90,180,270
.CWAngle = 90 * Loop1 'CW limit ie 90,180,270,360
.Angle = 90 * Loop1 'Actual angle ie 90,180,270,360
.Order = (2 ^ (NumPasses + 2)) / 4 * Loop1 'Where they appear in order of all spokes
'ie if there are 16 lines then the axis would be line 4,8,12 and 16
End With
Next

Me.Show 'Show form
With Picture1
.AutoRedraw = True 'Set to redraw if covered
.Width = Screen.TwipsPerPixelX * 300 'Set width based on screen res
.Height = Screen.TwipsPerPixelY * 300
Picture1.Scale (-150, -150)-(150, 150) 'scale pic to 300 x 300
End With

End Sub

Private Sub Command1_Click()
Dim CurrentPosn As Long 'Current line being worked on
Dim PreviousPosn As Long 'Previous line gives angle limits for current line
Dim TempAngle As Long 'Temp to store angle
Dim AngleRandDir As Integer 'Is new angle going to have a random offset
'in CCW or CW direction. Ie new angle is half way
'between previous angles then adjusted for variance
'of up to 10% (AngleRand) in direction chosen by this var

On Error Resume Next
With Picture1
.Cls 'Clear picture
.ForeColor = vbBlack 'Set forecolor
Picture1.Circle (0, 0), MaxRadius 'Draw starting circle
End With


'This section works out the starting points for lines in
'each new pass. So if doing 2 passes (16 lines) the first line
'in the 1st pass will be number 2 in order, the first in the
'second pass will be number 1 in order etc
'-----------------------------------------------------------------
ReDim OrderStart(1 To NumPasses)
ReDim OrderJump(1 To NumPasses)
For Loop1 = 0 To NumPasses
OrderStart(Loop1) = 2 ^ (NumPasses - Loop1) 'First line of this pass
OrderJump(Loop1) = 2 ^ (NumPasses - Loop1 + 1) 'Subsequent lines in
'this pass will be multiples of x
Next
'=================================================================

'This section calcs the angles of all spokes
'-----------------------------------------------------------------
For Loop1 = 1 To NumPasses 'Loop thru number of passes
For Loop2 = 1 To 2 ^ (Loop1 + 1) 'How many NEW lines in each pass
CurrentPosn = 2 ^ (Loop1 + 1) + Loop2 'Current line
'Where this line appears in the order of all lines
Points(CurrentPosn).Order = OrderStart(Loop1) + OrderJump(Loop1) * (Loop2 - 1)

AngleRandDir = -1 + 2 * Int(Rnd * 2) 'Get a random offset dir -1 or 1
If Loop1 = 1 Then 'First pass is diff from others because only one intermediate line
'-----------------------------------------------------------------
PreviousPosn = CurrentPosn - 4 'Get num of relative line in previous pass
'eg Line 5 is first line in pass 1. It checks
'Line 1 to find out the range of angles.. ie 0 to 90
With Points(CurrentPosn) 'With the new line
.Length = MaxRadius - Int(Rnd * LineRand) 'Random length
.CCWAngle = 90 * (PreviousPosn - 1) 'CCW limit for next pass
.CWAngle = 90 * PreviousPosn 'CW limit for next pass
'Actual angle of new spoke.
.Angle = .CCWAngle + (.CWAngle - .CCWAngle) / 2 + (.CWAngle - .CCWAngle) * (Rnd * AngleRand) / 100 * AngleRandDir
End With
'=================================================================
Else
'-----------------------------------------------------------------
PreviousPosn = (CurrentPosn + 1) \ 2 'Previous line to get angle
'limits from is current line divided by 2
'ie If on third pass lines 9 and 10
'use line 5 to get angle limits
With Points(PreviousPosn)
If CurrentPosn Mod 2 = 1 Then 'Are we creating a new line
'above or below the previous line
'If above get the angle limits
TempAngle = .CCWAngle + (.Angle - .CCWAngle) / 2 + (.Angle - .CCWAngle) * (Rnd * AngleRand) / 100 * AngleRandDir
Else
TempAngle = .Angle + (.CWAngle - .Angle) / 2 + (.CWAngle - .Angle) * (Rnd * AngleRand) / 100 * AngleRandDir
End If
End With

With Points(CurrentPosn)
.Length = MaxRadius - Int(Rnd * LineRand) 'Length of new line
If CurrentPosn Mod 2 = 1 Then
.CCWAngle = Points(PreviousPosn).CCWAngle
.Angle = TempAngle
.CWAngle = Points(PreviousPosn).Angle
Else
.CCWAngle = Points(PreviousPosn).Angle
.Angle = TempAngle
.CWAngle = Points(PreviousPosn).CWAngle
End If
End With
'=================================================================
End If
Next
Next
'=================================================================

'This section draws the actual spokes
'-----------------------------------------------------------------
For Loop1 = 1 To UBound(Points)
If Loop1 > 4 Then Picture1.ForeColor = vbBlue
With Points(Loop1)
.CurX = .Length * Cos(.Angle * (pi / 180))
.CurY = .Length * Sin(.Angle * (pi / 180))
Picture1.Line (0, 0)-(.CurX, .CurY)
PointsOrd(.Order).CurX = .CurX 'Where line is in order for connections
PointsOrd(.Order).CurY = .CurY
End With
Next
'=================================================================

'This section draws the actual connections between spokes
'-----------------------------------------------------------------
With Points(4) 'Starting point at 360 degrees
Picture1.CurrentX = .CurX
Picture1.CurrentY = .CurY
End With

Picture1.ForeColor = vbRed
For Loop1 = 1 To UBound(PointsOrd)
With PointsOrd(Loop1)
Picture1.Line -(.CurX, .CurY)
End With
Next
'=================================================================
End Sub

'Continued next post

beachbum
Jan 12th, 2002, 03:44 PM
How it works:
The number of passes determines how many lines you will draw. As an example we will use 2 passes

So to start with we have 4 lines for the axis (1 to 4). Pass 1 adds 4 lines for the 45 deg marks (randomly offset) (5 to 8) Pass 2 adds 8 lines for 22.5 deg marks (randomly offset) (9 to 16) giving a total of 16 lines

We also know the order of each of these lines
ie Line1 (original axis) is at 90deg and if there are 16 lines it must be the 4th
Line 5 (first line in pass1) is close to 45 deg and therefore must be 2nd in order
Line 9 (first in pass2) is close to 22.5 deg and is the first line in order
So the lines are created like this running clockwise


'Pass Line Number Order of that line
'0 01 (90) 04
'0 02 (180) 08
'0 03 (270) 12
'0 04 (360) 16

'1 05 (45) 02 (4 between each in order)
'1 06 (135) 06
'1 07 (225) 10
'1 08 (315) 14

'2 09 (22.5) 01 '(2 between each in order)
'2 10 (67.5) 03
'2 11 (112.5) 05
'2 12 (157.5) 07
'2 13 (202.5) 09
'2 14 (247.5) 11
'2 15 (292.5) 13
'2 16 (335.5) 15


So first pass thru we create lines between 0 & 90, 90 & 180, 180 & 270, 270 & 360
We find the half way point (ie 45) and then adjust randomly by up to 10% up or down
Say the line becomes 42. The next pass thru would find lines (CCW) between 0 and 42 and between 42 and 90.

'Each pass is creating a new line randomly halfway between lines of previous pass
'The new angles are determined by the limits of the relative line in the previous pass.