We use a root Node2D and attach a script.
In the script, we draw a polygon of N equal sides, around a center point. As N becomes large, we approach a circle. In the function draw_regular_polygon, we use dictionary col. In the loop we initial or update three keys 'r', 'g', and 'b'. We could have read the elements as col['r'] or col.r, etc.
This is the script to draw the regular polygon:
extends Node2D
func _ready():
randomize()
func _draw():
var cen = Vector2(100, 100)
var rad = 90
var N = 8
draw_regular_polygon(cen, rad, N)
func draw_regular_polygon(cen, rad, N):
if N < 3: return
var col = {}
var cols
var ang
var x
var y
var points = PoolVector2Array()
var colors = PoolColorArray()
for i in range(N + 1):
col['r'] = rand_range(0,1)
col['g'] = rand_range(0,1)
col['b'] = rand_range(0,1)
cols = Color(col.r, col.g, col.b)
ang = i * (2 * PI / N)
x = rad * cos(ang)
y = rad * sin(ang)
points.append(cen + Vector2(x, y))
colors.append(cols)
draw_polygon(points, colors)
The 8-sided polygon is shown below:
No comments:
Post a Comment