|
Post by tsh73 on Sept 19, 2019 13:55:26 GMT -5
Today my student was plotting polar chart (as programming exercise) Here the problem we got (I finally guessed, but it was after class). I wonder what you say So. polar chart: you change polar angle fi from 0 to 2*pi, you have r(fi), and you plot that point. If r=constant (say 2), you'll get perfect circle. Like this. Now student's task was r=(fi mod a) + b Let's take r=(fi mod 1) + 1 Let's run it (find and uncomment this line in code below) Whoa. Now, the question is: why first image is nice closed line, and second one is not? (and the second question - how to fix program so we got nice symmetrical figure). Code follows (based on Graphics 101 – plotting a function) nomainwin global winW, winH, xmin, xmax, ymin, ymax open "test" for graphics_nsb_nf as #gr #gr "trapclose [quit]" #gr "down"
#gr "home" #gr "posxy w h" winW=2*w: winH=2*h
pi=acs(-1) 'bounds are known xmin=-3: xmax=3 ymin=-3: ymax=3
nPoints=300 'we have only this much screen dots in X range dfi=2*pi/nPoints 'so this will be step in math coordinates
'now we just - plot function. Note same loop #gr "color red" #gr "size 3" r=2 x=r*cos(fi) y=r*sin(fi) #gr "set ";sx(x);" ";sy(y) 'just set first dot for fi=0 to 2*pi step dfi r=2 'r=(fi mod 1) + 1 x=r*cos(fi) y=r*sin(fi) #gr "goto ";sx(x);" ";sy(y) 'then connect dots next
'and finally, add axis #gr "color black" #gr "size 1" #gr "line ";sx(xmin);" ";sy(0);" ";sx(xmax);" ";sy(0) #gr "line ";sx(0);" ";sy(ymin);" ";sx(0);" ";sy(ymax)
'labeling #gr "place ";sx(0)+5;" ";sy(0)-5 #gr "\0,0" #gr "place ";sx(xmax)-20;" ";sy(0)-5 #gr "\X" #gr "place ";sx(0)+5;" ";sy(ymax)+20 #gr "\Y"
#gr "flush" #gr, "getbmp drawing 1 1 ";winW;" ";winH bmpsave "drawing", "graph.bmp"
wait
[quit] close #gr end
'To translate X from interval [a,b] to [c,d] we'll do (X-a)/(b-a)*(d-c)+c. 'create two functions: sx(x) and sy(y) function sx(x) sx=(x-xmin)/(xmax-xmin)*winW end function function sy(y) sy=winH-(y-ymin)/(ymax-ymin)*winH 'Y is inverted, so winH-... end function
|
|
|
Post by Carl Gundel on Sept 19, 2019 15:53:46 GMT -5
Today my student was plotting polar chart (as programming exercise) Here the problem we got (I finally guessed, but it was after class). The code you posted seems to be the one for drawing the circle.
|
|
|
Post by tsh73 on Sept 19, 2019 16:04:54 GMT -5
The code you posted seems to be the one for drawing the circle. Actually, both: it was written
|
|
|
Post by tenochtitlanuk on Sept 19, 2019 16:18:42 GMT -5
( Allegedly) a state in the US once legislated that pi =22/7. So why should 2 pi not =6 ??!!
|
|
|
Post by tenochtitlanuk on Sept 20, 2019 7:01:43 GMT -5
...and hereś the output of a modified form of the code.. As a further challenge- fill the gear shape in colour draw two meshing rotating gears modify the tooth shape to the correct shape rather than a bent sine wave!
|
|
|
Post by tenochtitlanuk on Sept 20, 2019 12:38:57 GMT -5
|
|
|
Post by tenochtitlanuk on Sept 25, 2019 9:18:53 GMT -5
OK got meshing working... if a little flashy when running. nomainwin
global hdc, pi
WindowWidth =600 WindowHeight =600
open "test" for graphics_nsb_nf as #gr
#gr "trapclose [quit]" #gr "down"
targetcolor =0 ' this is the colour of the outline to fill out to. c =0
hw =hwnd( #gr) ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< calldll #user32, "GetDC", hw as ulong, hdc as ulong ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pi =acs( -1)
#gr "cls ; down ; fill 180 180 60 ; up ; size 2"
phase =0
do
#gr "cls ; down ; fill 180 180 180 ; up ; color 0 0 0"
call gear 385, 210, 30, "255 180 60", phase, "CCW" call gear 107, 229, 15, "230 160 80", phase, "CW"
#gr "flush"
#gr "getbmp drawing 1 1 580 570" bmpsave "drawing", "meshing" +right$( "000" +str$( c), 3) +".bmp" c =c +1
scan
calldll #kernel32, "Sleep", 200 as long, ret as void
phase =phase +pi /10
loop until phase >2 *pi
wait
sub gear xPos, yPos, numTeeth, phillCol$, phase, sense$ calcdRadius =int( numTeeth *6) ratio =1'30 /numTeeth
r =calcdRadius +10 *sin( numTeeth *phase) #gr "up ; goto "; xPos +r *cos( phase); " "; yPos +r *sin( phase)
for phi =0 to 2 *pi +0.1 step 0.02 r =calcdRadius +15 *sin( numTeeth *phi +phase *ratio)
if sense$ ="CW" then x =xPos +r *cos( phi) y =yPos +r *sin( phi) else x =xPos +r *cos( phi) y =yPos -r *sin( phi) end if
#gr "goto "; int( x); " "; int( y) #gr "down" 'calldll #kernel32, "Sleep", 2 as long, ret as void
next phi
#gr "backcolor 200 200 200" #gr "up ; goto "; xPos; " "; yPos; " ; down ; circlefilled 10" xVar =xPos +20 yVar =yPos
#gr "backcolor "; phillCol$
calldll #gdi32, "ExtFloodFill",_ hdc as ulong,_ xVar as long,_ yVar as long,_ targetcolor as long,_ _FLOODFILLBORDER as long,_ ' ' ie fill out 'til targetcolour is met result as long
#gr "up" end sub
[quit] close #gr calldll #user32, "ReleaseDC", hw as ulong, hdc as ulong, ret as void 'release the DC <<<<<<<<<<<<<<<<<<<<< end
|
|
|
Post by tsh73 on Sept 25, 2019 12:03:48 GMT -5
Ohhhh that's cute (if this could be said about program). Will examine it - really interesting how you synched them.
|
|
|
Post by tsh73 on Sept 26, 2019 4:18:41 GMT -5
Trying to understand how/why it works end up with this.
Why red dot jumps then changing number of teeths? So I still not completely done with "phase" thing.
'running gears of tenochtitlanuk 'modif by tsh73 for being JB-friendly 'and a bit more ;) nomainwin
global hdc, pi
WindowWidth =600 WindowHeight =600
open "running gears" for graphics_nsb_nf as #gr
#gr "trapclose [quit]" #gr "down"
targetcolor =0 ' this is the colour of the outline to fill out to. c =0
' hw =hwnd( #gr) ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ' calldll #user32, "GetDC", hw as ulong, hdc as ulong ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pi =acs( -1)
#gr "cls ; down ; fill 180 180 60 ; up ; size 2"
phase =0
dk=2 'teeth number changes by 2 - by 1 it goes teeth-on-teeth every other time k=0 do
#gr "cls ; down ; fill 180 180 180 ; up ; color 0 0 0"
' call gear 385, 210, 30, "255 180 60", phase, "CCW" ' call gear 107, 229, 15, "230 160 80", phase, "CW" 'changing teeths number works! So let's change it: call gear 385, 210, 34-k, "255 180 60", phase, "CCW" call gear 107, 229, 11+k, "230 160 80", phase, "CW"
'#gr "flush"
' #gr "getbmp drawing 1 1 580 570" ' bmpsave "drawing", "meshing" +right$( "000" +str$( c), 3) +".bmp" #gr "discard" c =c +1 if c mod 30=0 then k=k+dk if k>25 or k <0 then dk = 0 - dk end if
scan
'Calldll #kernel32, "Sleep", 200 as long, ret as void ' timer 20, [nxt] wait [nxt] timer 0
phase =phase +pi /10
loop until 0'phase >2 *pi
wait
sub gear xPos, yPos, numTeeth, phillCol$, phase, sense$ if sense$ ="CW" then sgn=1 else sgn=-1 end if calcdRadius =int( numTeeth *6) ratio =1'30 /numTeeth
r =calcdRadius +15 *sin( numTeeth *(phi + phase/numTeeth)) 'same formula as inside #gr "set "; xPos +r *cos( 0); " "; yPos +r *sin( 0) 'initial angle is 0 #gr "down"
h=3/calcdRadius for phi =0 to 2 *pi +0.1 step h'0.02 'r =calcdRadius +15 *sin( numTeeth *phi +phase *ratio) 'original, but ratio actually 1 'r =calcdRadius +15 *sin( numTeeth *phi +phase) 'actually same formula, but shows that phase actually (phase/numTeeth) r =calcdRadius +15 *sin( numTeeth *(phi + phase/numTeeth))
x =xPos +r *cos( phi) y =yPos +sgn*r *sin( phi)
#gr "goto "; int( x); " "; int( y) 'calldll #kernel32, "Sleep", 2 as long, ret as void next phi
'add red dot on a circle 'shows that phase actually (phase/numTeeth) #gr "color red; size 6; set ";xPos+calcdRadius*cos( phase/numTeeth); " "; yPos-sgn*calcdRadius*sin( phase/numTeeth) #gr "color black; size 2"
#gr "backcolor 200 200 200" #gr "up ; goto "; xPos; " "; yPos; " ; down ; circlefilled 10" xVar =xPos +20 yVar =yPos
#gr "backcolor "; phillCol$
#gr "up" end sub
[quit] close #gr end
|
|
|
Post by tenochtitlanuk on Sept 26, 2019 14:41:52 GMT -5
It really messed with the mental gears in my head! It's really off-putting when you get the ratios/phase/rates wromg- teeth crossing over, wheels rotating the wrong way or at wrong speed. Can't spend any more time on it at present as I'm away from home- but intend to get back and will be interested in what you can create. You're ahead of me already- I intended to add an index dot to make it easy to see what was going on.
|
|
|
Post by tsh73 on Sept 26, 2019 16:16:39 GMT -5
Meanwhile, I got some (very) colored "gears": (second is first with more tooths actually) more here
|
|
|
Post by bluatigro on Sept 27, 2019 4:30:02 GMT -5
pi = 22 / 7 ? pi = 355 / 113 is more closer [ for if you have no gonio ]
|
|
|
Post by Carl Gundel on Sept 27, 2019 8:38:19 GMT -5
pi = 22 / 7 ? pi = 355 / 113 is more closer [ for if you have no gonio ] Indiana Pi :-D
|
|
|
Post by tenochtitlanuk on Sept 27, 2019 10:20:13 GMT -5
See my post earlier in the thread! ( Did you see my suggestions re pi/radians/degrees for LB5 on the JB forum?)
|
|
|
Post by Carl Gundel on Sept 27, 2019 10:53:18 GMT -5
See my post earlier in the thread! ( Did you see my suggestions re pi/radians/degrees for LB5 on the JB forum?) Didn't quite catch that. Funny that whole matter, and pretty much lunacy.
|
|