Kleine Bezierspielerei

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Firstdeathmaker

Betreff: Kleine Bezierspielerei

BeitragFr, Jun 06, 2008 17:49
Antworten mit Zitat
Benutzer-Profile anzeigen
Code: [AUSKLAPPEN]
SuperStrict

Type TPoint
   Field x:Float
   Field y:Float
   
   Function Create:TPoint(x:Float , y:Float)
      Local p:TPoint = New TPoint
      p.x = x
      p.y = y
      Return p
   End Function
   
   Method move(x:Float , y:Float)
      Self.set(Self.x + x, Self.y + y)
   End Method
   
   Method set(x:Float , y:Float)
      Self.x = x
      Self.y = y
   End Method
   
   Method draw(size:Float=1.0)
      DrawRect Self.x - size/2,Self.y - size/2,size,size
   End Method
End Type

Type TCurve
   Field point:TPoint[4]

   
   Function Create:TCurve(point1:TPoint , point2:TPoint , point3:TPoint , point4:TPoint)
      Local c:TCurve = New TCurve
      c.point[0] = point1
      c.point[1]  = point2
      c.point[2]  = point3
      c.point[3]  = point4
      Return c
   End Function

   Method getX:Float(t:Float)
      Return BezierCurve4(t , point[0] .x , point[1] .x , point[2] .x , point[3] .x)
   End Method
   
   Method getY:Float(t:Float)
      Return BezierCurve4(t , point[0] .y , point[1] .y , point[2] .y , point[3] .y)
   End Method
   
   Method draw(resolution:Int= 10)
      Local stepwidth:Float = 1.0 / resolution
      
      Local t0:Float = 0
      Local x:Float = getX(0)
      Local y:Float = getY(0)
      
      For Local i:Int = 1 To resolution
         Local t:Float = t0 + i * stepwidth
         Local nextx:Float = getX(t)
         Local nexty:Float = getY(t)
         DrawLine x,y,nextx,nexty
         x = nextx
         y = nexty
      Next
   End Method
   
   Method getDistance:Float(t0:Float , t1:Float , resolution:Int = 1)
      Local sum:Int = 0
      Local stepwidth:Float = (t0 - t1) / resolution
      For Local i:Int = 1 To resolution
         t1 = t0 + stepwidth * i
         Local x:Float = (Self.getX(t0) - Self.getX(t1))
         Local y:Float = (Self.getY(t0) - Self.getY(t1))
         sum:+ Sqr (x*x + y*y)
      Next
      Return sum
   End Method
      
End Type

Function BezierCurve4:Float(t:Float , p1:Float , p2:Float , p3:Float , p4:Float)
   Return p1*(1-t)^3+3*p2*t*(1-t)^2+3*p3*t^2*(1-t)+p4*t^3
End Function


Const GFX_WIDTH:Int = 800
Const GFX_HEIGHT:Int = 600
Graphics GFX_WIDTH , GFX_HEIGHT, 0
SetBlend lightblend
   Local points:TPoint[4]
   
   Global curvecount:Int = 7
   Global curve:TCurve[curvecount]
   Global curve2:TCurve[curvecount]
   
   For Local i:Int = 0 Until curvecount
      points[0] = TPoint.Create(0,0)
      points[1] = TPoint.Create(0,0)
      points[2] = TPoint.Create(0,0)
      points[3] = TPoint.Create(GFX_WIDTH * (i+0.5)/Float(curvecount),(GFX_HEIGHT + 6) * (i Mod 2) - 3)
      curve:TCurve[i] = TCurve.Create(points[0] , points[1] , points[2] , points[3])
      
      points[0] = TPoint.Create(0,0)
      points[1] = TPoint.Create(0,0)
      points[2] = TPoint.Create(0,0)
      points[3] = TPoint.Create(GFX_WIDTH * (i+0.5)/Float(curvecount),(GFX_HEIGHT + 6) * ((i+1) Mod 2) - 3)
      curve2:TCurve[i] = TCurve.Create(points[0] , points[1] , points[2] , points[3])
   Next

Local counter:Float = 0

Repeat
   counter:+ ((Sin(counter*2.99) + 1.0) / 6.0) + 0.6
   Cls
   
   For Local c:Int = 0 Until curvecount
   For Local p:Int = 0 To 2
      Local adding:Float = Float(p+1)
      Local adding2:Float = Float(p+1)
         curve[c].point[p].set((Sin(counter * adding) + 1) * GFX_WIDTH/2.0 , (Cos(counter * adding2)+1) * GFX_HEIGHT/2.0)
         curve2[c].point[p].set( (Cos(counter * adding) + 1) * GFX_WIDTH / 2.0 , (Sin(counter * adding2) + 1) * GFX_HEIGHT / 2.0)
   
         If P=0 curve2[c].point[p].set((Sin(counter * adding) + 1) * GFX_WIDTH/2.0 , (Cos(counter * adding2)+1) * GFX_HEIGHT/2.0)
   Next
   SetColor 255 * (Abs(Cos(counter / 3.0 + ((Float(c)/curvecount)*551) / 21))*0.8 + 0.2),255 * (Sin(counter / 2.0)*0.8 + 0.2),255*Sin((counter + ((Float(c)/curvecount)*555)) / 13)
   Local resolution:Float = 50
   SetAlpha 1
   SetLineWidth(1)
   curve[c].draw(resolution)
   curve2[c].draw(resolution)
   SetAlpha 0.5
   SetLineWidth(2)
   curve[c].draw(resolution)
   curve2[c].draw(resolution)
   SetAlpha 0.3
   SetLineWidth(3)
   curve[c].draw(resolution)
   curve2[c].draw(resolution)
   SetAlpha 0.3
   SetLineWidth(4)
   curve[c].draw(resolution)
   curve2[c].draw(resolution)
   Next
   
   Local x:Float = curve[0].point[0].x
   Local y:Float = curve[0].point[0].y
   Local r:Float
   'SetColor 255,255,255
   For Local i:Int = 0 To 10
      r = i * 2
      SetAlpha (1.0-(i/10.0))^2
      DrawOval x - r / 2.0 , y - r / 2.0 , r , r
   Next
   Flip
Until KeyHit(KEY_ESCAPE)
End



~Editiert~
Hab mal das Rot entfernt. MfG D2006
www.illusion-games.de
Space War 3 | Space Race | Galaxy on Fire | Razoon
Gewinner des BCC #57 User posted image
 

Ava

Gast

BeitragFr, Jun 06, 2008 19:12
Antworten mit Zitat
Irgendwie cool ^^ Very Happy

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group