Created
February 10, 2025 05:45
-
-
Save MaBecker/a1137846b5eb39f0a630b07c825387b5 to your computer and use it in GitHub Desktop.
javascript draw rectangle with or without rounded corners
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g.clear(); | |
// source https://schwarzers.com/algorithms/ | |
// Draw Circle as 8 segments | |
// { mx:x, my:y, r:radius,q:quarte } | |
drawCircleV1 = function(mx,my,r) { | |
let t1 = r / 16 | |
,t2 = 0 | |
,x = r | |
,y = 0 | |
; | |
while ( x >= y ) | |
{ | |
// 1. Quadrant | |
if (q == 1) { | |
// I 1/8 | |
g.setPixel( mx + x, my - y ); | |
// I 2/8 | |
g.setPixel( mx + y, my - x ); | |
} | |
// 2. Quadrant | |
if (q == 2) { | |
// II 3/8 | |
g.setPixel( mx - y, my - x ); | |
// II 4/8 | |
g.setPixel( mx - x, my - y ); | |
} | |
// 3. Quadrant | |
if ( q == 3 ) { | |
// III 5/8 | |
g.setPixel( mx - x, my + y ); | |
// III 6/8 | |
g.setPixel( mx - y, my + x ); | |
} | |
// 4. Quadrant | |
if (q == 4 ) { | |
// IV 7/8 | |
g.setPixel( mx + y, my + x ); | |
// IV 8/8 | |
g.setPixel( mx + x, my + y ); | |
} | |
y = y + 1; | |
t1 = t1 + y; | |
t2 = t1 - x; | |
if ( t2 >= 0 ) | |
{ | |
t1 = t2; | |
x = x - 1; | |
} | |
} | |
}; | |
// add w & h | |
drawCircleV2 = function(mx,my,r,w,h) { | |
let t1 = r / 16 | |
,t2 = 0 | |
,x = r | |
,y = 0 | |
,i = 0 | |
; | |
while ( x >= y ) | |
{ | |
// 1. Quadrant | |
// I 1/8 | |
g.setPixel( mx + w - r -r + x, my - y ); | |
// I 2/8 | |
g.setPixel( mx + w - r -r + y, my - x ); | |
// 2. Quadrant | |
// II 3/8 | |
g.setPixel( mx - y, my - x ); | |
// II 4/8 | |
g.setPixel( mx - x, my - y ); | |
// 3. Quadrant | |
// III 5/8 | |
g.setPixel( mx - x, my + h -r -r + y ); | |
// III 6/8 | |
g.setPixel( mx - y, my + h -r -r + x ); | |
// 4. Quadrant | |
// IV 7/8 | |
g.setPixel( mx + w -r -r + y, my + h -r -r + x ); | |
// IV 8/8 | |
g.setPixel( mx + w -r -r + x, my + h -r -r + y ); | |
y = y + 1; | |
t1 = t1 + y; | |
t2 = t1 - x; | |
if ( t2 >= 0 ) | |
{ | |
t1 = t2; | |
x = x - 1; | |
} | |
i++; | |
} | |
print(i); | |
}; | |
// two rect & four circle | |
drawRectV1 = function(opt,y1,x2,y2) { | |
let x = opt.x | |
,y = opt.y | |
,w = opt.w | |
,h = opt.h | |
,r = opt.r | |
; | |
g.drawRect(x+r,y,x+w-r,y+h); | |
g.drawRect(x,y+r,x+w,y+h-r); | |
// left top | |
g.drawCircle(x+r,y+r,r); | |
// left bot | |
g.drawCircle(x+r,y+h-r,r); | |
// right top | |
g.drawCircle(x+w-r,y+r,r); | |
// right bot | |
g.drawCircle(x+w-r,y+h-r,r); | |
}; | |
// one circle & four lines | |
drawRectV2 = function(opt,y1,x2,y2) { | |
let x = opt.x | |
,y = opt.y | |
,w = opt.w | |
,h = opt.h | |
,r = opt.r | |
; | |
// reverse clock draw | |
g.drawLine(x+r,y,x+w-r,y); | |
// left top | |
drawCircleV2(x+r,y+r,r,w,h); | |
g.drawLine(x,y+r,x,y+h-r); | |
g.drawLine(x+r,y+h,x+w-r,y+h); | |
g.drawLine(x+w,y+h-r,x+w,y+r); | |
}; | |
// four lines & one circle integrated | |
drawRectV3 = function(opt,y1,x2,y2) { | |
let x = opt.x | |
,y = opt.y | |
,w = opt.w | |
,h = opt.h | |
,r = opt.r || 0 | |
; | |
print(); | |
if (r == 0 ){ | |
g.drawRect(opt); | |
} else { | |
g.drawLine(x+r,y,x+w-r,y); | |
g.drawLine(x,y+r,x,y+h-r); | |
g.drawLine(x+r,y+h,x+w-r,y+h); | |
g.drawLine(x+w,y+h-r,x+w,y+r); | |
let mx = x+r | |
,my = y+r | |
,mx1 = x + w - r | |
,my1 = y + r | |
,t1 = r / 16 | |
,t2 = 0 | |
,xr = r | |
,yr = 0 | |
; | |
mx = x+r; my = y+r; | |
mx1 = x + w - r; | |
my1 = y + r; | |
my3 = y + h - r; | |
mx4 = y + w - r; | |
my4 = y + h - r; | |
while ( xr >= yr ) | |
{ | |
// 1. Quadrant | |
// I 1/8 | |
g.setPixel( mx1 + xr, my - yr ); | |
// I 2/8 | |
g.setPixel( mx1 + yr, my - xr ); | |
// 2. Quadrant | |
// II 3/8 | |
g.setPixel( mx - yr, my - xr ); | |
// II 4/8 | |
g.setPixel( mx - xr, my - yr ); | |
// 3. Quadrant | |
// III 5/8 | |
g.setPixel( mx - xr, my3 + yr ); | |
// III 6/8 | |
g.setPixel( mx - yr, my3 + xr ); | |
// 4. Quadrant | |
// IV 7/8 | |
g.setPixel( mx4 + yr, my4 + xr ); | |
// IV 8/8 | |
g.setPixel( mx4 + xr, my4 + yr ); | |
yr = yr + 1; | |
t1 = t1 + yr; | |
t2 = t1 - xr; | |
if ( t2 >= 0 ) | |
{ | |
t1 = t2; | |
xr = xr - 1; | |
} | |
} | |
} | |
}; | |
// four lines & circle integrated, optimized mx, my calculation | |
drawRectV4 = function(opt,y1,x2,y2) { | |
let x = opt.x | |
,y = opt.y | |
,w = opt.w | |
,h = opt.h | |
,r = opt.r || 0 | |
; | |
if (r == 0 ){ | |
g.drawRect(opt); | |
} else { | |
g.drawLine(x+r,y,x+w-r,y); | |
g.drawLine(x,y+r,x,y+h-r); | |
g.drawLine(x+r,y+h,x+w-r,y+h); | |
g.drawLine(x+w,y+h-r,x+w,y+r); | |
let mx = x+r | |
,my = y+r | |
,mx1 = x + w - r | |
,my1 = y + r | |
,t1 = r / 16 | |
,t2 = 0 | |
,xr = r | |
,yr = 0 | |
; | |
mx = x+r; my = y+r; | |
mx1 = x + w - r; | |
my1 = y + r; | |
my3 = y + h - r; | |
mx4 = y + w - r; | |
my4 = y + h - r; | |
while ( xr >= yr ) | |
{ | |
// 1. Quadrant | |
// I 1/8 | |
g.setPixel( mx1 + xr, my - yr ); | |
// I 2/8 | |
g.setPixel( mx1 + yr, my - xr ); | |
// 2. Quadrant | |
// II 3/8 | |
g.setPixel( mx - yr, my - xr ); | |
// II 4/8 | |
g.setPixel( mx - xr, my - yr ); | |
// 3. Quadrant | |
// III 5/8 | |
g.setPixel( mx - xr, my3 + yr ); | |
// III 6/8 | |
g.setPixel( mx - yr, my3 + xr ); | |
// 4. Quadrant | |
// IV 7/8 | |
g.setPixel( mx4 + yr, my4 + xr ); | |
// IV 8/8 | |
g.setPixel( mx4 + xr, my4 + yr ); | |
yr = yr + 1; | |
t1 = t1 + yr; | |
t2 = t1 - xr; | |
if ( t2 >= 0 ) | |
{ | |
t1 = t2; | |
xr = xr - 1; | |
} | |
} | |
} | |
}; | |
//==== some test calls ====// | |
// drawCircle(100,100,30,1); | |
// drawRectV1({x:20,y:20,w:60,h:40,r:7}); | |
//drawRectV3({x:20,y:20,w:150,h:150,r:20}); | |
//drawRectV4(20,20,170,170); | |
drawRectV4({x:20,y:20,w:150,h:150,r:75}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment