Old basic port

I want to draw a mathematical figure, a so-called asteroid:

This is the old basic code:

[code]Rem ASTEROID

Screen 12:CLS:PI=4ATN(1)
Window(-1.6,-1.2)-(1.6,1.2)
Locate 1,1: Input"Amount of lines between 32 and 128 = ",N
N=2
INT(N/2): IF N<32 THEN N=64
Locate 1,1: Print Space$(80)

For I=0 To N-1
T=2PiI/N
Line(Cos(T),0)-(0,Sin(t))
next I
End[/code]

This code is in the paint event of a Canvas:

[code]Dim Pi as Double =4*ATan(1)
dim A as Integer = 64
dim N as Integer
dim T as Integer

N=2*Round(A/2)

If N<32 then
N = 64
end if

g.ForeColor = &c0000FF00

for I as Integer = 0 to N-1
T = 2PiI/N

g.DrawLine(Cos(T),0,0,Sin(T))

next[/code]

I omitted the input and took a fixed value, but I don’t see anything.
[Edit] something is drawn, but too small.

Any idea how I can enlarge the values?

T needs to be a double, and then you can scale things up.

I see something, a dot. That’s because the values of Sin(T) and Cos(T) alternate between 0 and 1.

Change your code like this and trace it yourself. That’s the best way to identify problems like this.

  Dim Pi as Double =4*REALbasic.ATan(1)
  
  dim A as Integer = 64
  dim N as Integer
  dim T as Integer
  
  N=2*Round(A/2)
  
  If N<32 then
    N = 64
  end if
  
  g.ForeColor = &c0000FF00
  
  for I as Integer = 0 to N-1
    
    T = 2*Pi*I/N
    
    dim tCos as integer = REALbasic.Cos( T )
    dim tSin as integer = REALbasic.Sin( T )
    g.DrawLine(tCos,0,0,tSin)
    
  next

Oh Kem, why do you have to make me look bad by trying to teach the OP something? LOL

(by the way Alexander, I did just about what Kem did to find it but I used "System.DebugLog str(sin(T)) + “, " + str(cos(T))” to figure out what was going on.)

:slight_smile:

Hi,

It was meant for my son, to to show him how images can be drawn with little code. I willlook into your suggestions.

By-the-way, what us the difference between a standard Cos() function and a Realbasic.Cos()?

They are exactly the same function. Realbasic is the default namespace.

Yes. I have a plugin that also defines Cos so I have to use the namespace to avoid a conflict.

This is why everyone, including plugin authors, should use a namespace instead of just putting methods into the global namespace.

Then you could have
MBS.Cos()
Realbasic.Cos()
Tekinay.Cos()
and it would be unambiguous

Can I ask why your plugin needs to define Cos?

You’d have to ask the plugin’s author, Bob Delaney. It’s either his fp plugin or decimal plugin, I don’t recall at the moment.