Notifications
Clear all

[Closed] use setpixels draw a Solid circle to bitmap

hi guys! there is a problem , how to use setpixels draw a Solid circle to bitmap?


rollout cc "" width:200 height:200
(
	imgtag itd "" pos:[0,0] width:200 height:200
	
	on cc open do
	(
		bmpd=bitmap 200 200 color:red
		
		setpixels bmpd [100,100] #(green)
		
		itd.bitmap=bmpd
		)
	)
createdialog  cc

i want to draw a solid circle the green pixel is the center and radius is 20 or it can change other value, is there any way to draw a perfect solid circle with use setpixels,
maybe want some expression? :curious:

6 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

try to start from a circle equation…

Here is something to play with:

(
global CircleDraw_Dialog
try(destroyDialog CircleDraw_Dialog)catch()
local theSize = 150.0
rollout CircleDraw_Dialog "Draw Circle"
(
	local thePaper, theBmp --local variables to hold the image buffers
	
	--User Interface Controls
	bitmap bmp_display width:theSize height:theSize
	colorpicker clr_forecolor "F:" color:red modal:false across:2 align:#right
	colorpicker clr_backcolor "B:" color:white modal:false align:#right
	spinner spn_radius "Radius:" type:#integer range:[0,theSize*0.5,30] fieldwidth:50 align:#right

	fn updateBitmap updatePaper:false =
	(	
		if updatePaper do  --reset paper if needed
			thePaper = bitmap theSize theSize color:clr_backcolor.color
		copy thePaper theBmp --copy paper into image buffer
		for i = -90 to 90 by (55.0/spn_radius.value) do --loop half a circle with adjusted step
		(
			local theX = spn_radius.value * (cos i) 
			setPixels theBmp [0.5*theSize - theX, 0.5*theSize + spn_radius.value *(sin i)] (for j = 1 to theX*2 collect clr_forecolor.color)
		)
		bmp_display.bitmap = theBmp --display the image buffer
	)
	--Event handlers:
	on spn_radius changed val do updateBitmap()
	on clr_forecolor changed val do updateBitmap()
	on clr_backcolor changed val do updateBitmap updatePaper:true
	on CircleDraw_Dialog open do 
	(
		theBmp = bitmap theSize theSize
		updateBitmap updatePaper:true
	)
)--end rollout
createDialog CircleDraw_Dialog theSize (theSize+60)
)

This is very interesting, Bobo,
As the Japanese flag.

The default colors were indeed intentional. I started with black and decided to go Rising Sun instead. But you can change the colors afterwards…

Now try to extend it to do an ellipse!

I should go to sleep, but I haven’t done “recreational scripting” in ages.
So here is the version with an Ellipse option:

(
global CircleDraw_Dialog
try(destroyDialog CircleDraw_Dialog)catch()
local theSize = 150.0
rollout CircleDraw_Dialog "Draw Circle"
(
	local thePaper, theBmp --local variables to hold the image buffers
	
	--User Interface Controls
	bitmap bmp_display width:theSize height:theSize
	colorpicker clr_forecolor "F:" color:red modal:false across:2 align:#right
	colorpicker clr_backcolor "B:" color:white modal:false align:#right
	spinner spn_radius "Radius X:" type:#integer range:[0,theSize*0.5,30] fieldwidth:40 align:#right offset:[-12,0]
	spinner spn_radius2 "Radius Y:" type:#integer range:[0,theSize*0.5,30] fieldwidth:40 align:#right offset:[-12,0]
	checkbutton chk_lockRadius "]" height:37 width:20 align:#right offset:[9,-42] checked:true

	fn updateBitmap updatePaper:false =
	(	
		if updatePaper do  --reset paper if needed
			thePaper = bitmap theSize theSize color:clr_backcolor.color
		copy thePaper theBmp --copy paper into image buffer
		for i = -90 to 90 by (55.0/spn_radius2.value) do --loop half a circle with adjusted step
		(
			local theX = spn_radius.value * (cos i) 
			setPixels theBmp [0.5*theSize - theX, 0.5*theSize + spn_radius2.value *(sin i)] (for j = 1 to theX*2 collect clr_forecolor.color)
		)
		bmp_display.bitmap = theBmp --display the image buffer
	)
	--Event handlers:
	on spn_radius changed val do 
	(
		if chk_lockRadius.checked do spn_radius2.value = val
		updateBitmap()
	)
	on spn_radius2 changed val do 
	(
		if chk_lockRadius.checked do spn_radius.value = val
		updateBitmap()
	)
	on chk_lockRadius changed state do 
	(
		if state do spn_radius2.value = spn_radius.value 
		updateBitmap()
	)		
	on clr_forecolor changed val do updateBitmap()
	on clr_backcolor changed val do updateBitmap updatePaper:true
	on CircleDraw_Dialog open do 
	(
		theBmp = bitmap theSize theSize
		updateBitmap updatePaper:true
	)
)--end rollout
createDialog CircleDraw_Dialog theSize (theSize+90)
)

thanks very much!guys! it really helpful for me!!