How to draw a star by using sin() cos() ?

Just starting out? Need help? Post your questions and find answers here.
Ralf
Enthusiast
Enthusiast
Posts: 203
Joined: Fri May 30, 2003 1:29 pm
Location: Germany

How to draw a star by using sin() cos() ?

Post by Ralf »

how to draw a star with sin() or cos() like in old demos end of the 90th?
the stars are available in the RSI demomaker package and looks something like this (bad example):

often seen with 8 lines (like an + and an x joined)

Code: Select all

            #
            #
            #
          ######
       ############
##########################
       ############
          ######
            #
            #
            #

Last edited by Ralf on Sat Sep 24, 2005 11:15 am, edited 1 time in total.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

viewtopic.php?t=13037

Here is a working demo of that function:

Code: Select all

;Cogged function:
; Author: Psychophanta
; Date: 5 Nov 2004

;-INITS:
bitplanes.b=32:RX.w=1024:RY.w=768:#PI=3.14159265
If InitMouse()=0 Or InitSprite()=0 Or InitKeyboard()=0
  MessageRequester("Error","Can't access DirectX",0)
  End
EndIf
While OpenScreen(RX.w,RY.w,bitplanes.b,"")=0
  If bitplanes.b>16:bitplanes.b-8
  ElseIf RY.w>600:RX.w=800:RY.w=600
  ElseIf RY.w>480:RX.w=640:RY.w=480
  ElseIf RY.w>400:RX.w=640:RY.w=400
  ElseIf RY.w>240:RX.w=320:RY.w=240
  ElseIf RY.w>200:RX.w=320:RY.w=200
  Else:MessageRequester("VGA","Can't open Screen!",0):End
  EndIf
Wend

Procedure Star(x.l,y.l,intrad.l,extrad.l,n.l,inclination.l,phase.f,color.l)
  alpha.f=2*#PI/n
  i.f=inclination/20-1/2
  For t.l=1 To n:d1.f=(t-1)*alpha+phase:d2.f=t*alpha+phase:d3.f=(t+i)*alpha+phase
    LineXY(x+intrad*Cos(d1),y+intrad*Sin(d1),x+extrad*Cos(d3),y+extrad*Sin(d3),color)
    LineXY(x+intrad*Cos(d2),y+intrad*Sin(d2),x+extrad*Cos(d3),y+extrad*Sin(d3),color)
  Next
EndProcedure

r1.l=70:r2.l=90:p.l=3
;-MAIN:
MouseLocate(333,333)
Repeat
  ClearScreen(0,0,0)
  ExamineKeyboard()
  ExamineMouse():mx.l=MouseX():my.l=MouseY()
  StartDrawing(ScreenOutput())
  Star(RX/2,RY/2,r1.l,r2.l,p,0,pha.f,$aaaaaa)
  pha+MouseDeltaX()*0.01:r2-MouseDeltaY()
  If MouseButton(1):While MouseButton(1):Delay(16):ExamineMouse():Wend
    p+1:If p>80:p=80:EndIf
  EndIf
  If MouseButton(2):While MouseButton(2):Delay(16):ExamineMouse():Wend
    p-1:If p<2:p=2:EndIf
  EndIf
  StopDrawing()
  FlipBuffers():Delay(16)
Until KeyboardPushed(#PB_Key_Escape)
You can test changing inclination parameter, etc.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Ralf
Enthusiast
Enthusiast
Posts: 203
Joined: Fri May 30, 2003 1:29 pm
Location: Germany

Post by Ralf »

i need a routine that plot a star like the one on this screenshot http://strider.mjjprod.free.fr/dck/ryuse1.gif (without the long line)

version 1) looks like " + "
version 2) looks like " + " and " x " merged


:?: :roll:
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

@Ralf:
maybe you can find such an algo/routine using google and search for any old skool related coding stuff :?: maybe someone here knows how to do such old coding stuff!? :wink:
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Ralf:
I gave you a versatile function to draw a star: at viewtopic.php?t=13037

Use a little bit your imagination.

This is a fast example:

Code: Select all

;-INITS: 
bitplanes.b=32:RX.w=1024:RY.w=768:#PI=3.14159265 
If InitMouse()=0 Or InitSprite()=0 Or InitKeyboard()=0 
  MessageRequester("Error","Can't access DirectX",0) 
  End 
EndIf 
While OpenScreen(RX.w,RY.w,bitplanes.b,"")=0 
  If bitplanes.b>16:bitplanes.b-8 
  ElseIf RY.w>600:RX.w=800:RY.w=600 
  ElseIf RY.w>480:RX.w=640:RY.w=480 
  ElseIf RY.w>400:RX.w=640:RY.w=400 
  ElseIf RY.w>240:RX.w=320:RY.w=240 
  ElseIf RY.w>200:RX.w=320:RY.w=200 
  Else:MessageRequester("VGA","Can't open Screen!",0):End 
  EndIf 
Wend 

Procedure Star(x.l,y.l,intrad.l,extrad.l,n.l,inclination.l,phase.f,color.l) 
  alpha.f=2*#PI/n 
  i.f=inclination/20-1/2 
  For t.l=1 To n:d1.f=(t-1)*alpha+phase:d2.f=t*alpha+phase:d3.f=(t+i)*alpha+phase 
    LineXY(x+intrad*Cos(d1),y+intrad*Sin(d1),x+extrad*Cos(d3),y+extrad*Sin(d3),color) 
    LineXY(x+intrad*Cos(d2),y+intrad*Sin(d2),x+extrad*Cos(d3),y+extrad*Sin(d3),color) 
  Next 
EndProcedure 

;-MAIN: 
MouseLocate(333,333) 
  ClearScreen(0,0,0) 
  StartDrawing(ScreenOutput()) 
  r1.f=0 
  For t.l=50 To 1 Step -1 
    r1.f+0.3:r2.f=r1.f*5 
    Star(RX/2,RY/2,r1.f,r2.f,8,0,#PI/8,t*$050403)
  Next 
  StopDrawing() 
  FlipBuffers(0) 
Repeat:ExamineKeyboard():Delay(16)
Until KeyboardPushed(#PB_Key_Escape)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Post by dobro »

super!!

it's a pity that the triangle is not aligned on the square!!

to direct in degree rotation?


but :

currently the superposition of a triangle and a cross-section shows:


Image


whereas it am cool to have:

Image



to direct in degree rotation?

Thank you
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Post by dobro »

multi-polygone whit the rotation in Degres :D



Code: Select all

#dobro=1
#Police=1
#Sprite=1
#PI=3.1415926
Declare polygone(x.l,y.l,taille.l,angle.l,rotation.f,color.l,t)
; ***********************************
Resultat = InitSprite()
FontID = LoadFont(#Police, "arial", 18, #PB_Font_Bold )
EcranX = GetSystemMetrics_(#SM_CXSCREEN):;=largeur de l'ecran
EcranY = GetSystemMetrics_(#SM_CYSCREEN):;=hauteur de l'ecran
  WindowID = OpenWindow(1, 0, 0, EcranX, EcranY,  #PB_Window_SystemMenu|#PB_Window_BorderLess |#PB_Window_ScreenCentered , "hello")
  
  WindowID = WindowID(1)
  Result = OpenWindowedScreen(WindowID,0,0,800, 600, 1, 0,0)
  
  
  
  Resultat = InitMouse() 
  Repeat
    ExamineMouse()
    Event=WindowEvent() 
    If MouseButton(2)
      End
    EndIf
    
    
    
    ;Star(x.l,y.l,intrad.l,n.l,phase.f,color.l)
    
    x.l=400 ;location x
    y.l=300 ; location y
    taille.l=100
    angle.l=4 ; pour un triangle  4 pour un carre , 8 pour un octogone ect ...
    rotation.f=180
    
    color.l=RGB($FF,$FF,$80)
    For t=0 To 360
    ; Delay (200)
    rotation.f=t 
      polygone(x.l,y.l,taille.l,angle.l,rotation.f,color.l,t)
     FlipBuffers():; affiche l'ecran
      ClearScreen(0,0,0) 
      ExamineMouse()
      Event=WindowEvent() 
      If MouseButton(2)
        End
      EndIf
    Next t
    

    
    
  Until Event=#PB_Event_CloseWindow
  
  
  
  
  Procedure  polygone(x.l,y.l,taille.l,angle.l,rotation.f,color.l,t)
    If angle.l=4 

    Else
    
    EndIf
     rotation.f= rotation.f*0.01745329
    StartDrawing(ScreenOutput() )
    DrawText(StrF( t))
    alpha.f=2*(#PI/angle)
    For t.l=1 To angle
      d1.f=(t-1)*alpha+rotation.f
      d3.f=t*alpha+rotation.f
      LineXY(x+taille*Sin(d1),y+taille*Cos(d1),x+taille*Sin(d3),y+taille*Cos(d3),color)
    Next
    StopDrawing()
  EndProcedure 


Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Post Reply