TNTDisk is a staple in our graphic library. When it was first introduced, we wrote it using 'function' type designs. Since then, we've upgraded to a 'class' design. In our Python3 class with 'Tracy the Turtle' we've even used OOP to get a Pythonic TNTDisk! All that code is listed below so you can compare the various styles and languages.
JavaScript Function Perspective
//---------------------- TNTDisk --------------------------
function TNTDisk(center, radius, fcolr, stroke) {
this.center = center; //a TNTPoint
this.h = center.x; //center of the disk (h,k)
this.k = center.y;
this.r = radius; //radius of the disk
this.fcolr = fcolr;
this.stroke = stroke; //a TNTStroke (provides border color/thickness)
this.showCenter = false;
//updated reference below to tntColor from tcaColor
if (stroke != null) this.accentColor = stroke.tntColor;
else this.accentColor = black;
//member methods
this.drawTNTDisk = drawTNTDisk;
this.setCenter = setCenter;
this.drawCenter = drawCenter; //added 12/10/13
this.setFillColor = setFillColor; //added 12/11/13
}//end TNTDisk
function drawTNTDisk(context) {
//in case center was changed in code, reset the h,k
this.h = this.center.x;
this.k = this.center.y;
context.beginPath();
context.arc(this.h, this.k, this.r, 0, Math.PI * 2, true);
if (this.fcolr != null) {
context.fillStyle = this.fcolr.hex;
context.fill();
}
if (this.stroke != null) {
context.strokeStyle = this.stroke.scolr;
context.lineWidth = this.stroke.lineWidth;
context.stroke();
}
context.closePath();
if (this.showCenter) {
this.drawCenter(context, this.accentColor);
}
}//end drawTNTDisk
//----------------------- end TNTDisk -------------------------
JavaScript Class Perspective
Coming Soon!
Python Class Perspective
class TNTDisk:
#--- constructor (initializer, dunder method) ---
def __init__(self, coord, radius, fcolr, strk):
self.coord = coord
self.x = coord[0]
self.y = coord[1]
self.radius = radius
self.fcolr = fcolr
self.strk = strk
#end __init__
#--- utility methods ---
def prepare_to_draw(self):
penup()
setposition(self.x, self.y)
right(90)
forward(self.radius)
left(90) #face west
#end prepare_to_draw
def return_home(self):
#move tracy back to original posn
penup()
left(90)
forward(self.radius)
right(90)
color("black")
#end return_home
def draw_center(self):
a_strk = None
if self.strk == None:
a_strk = TNTStroke("black", 1)
else:
a_strk = self.strk
cntr = TNTDisk(self.coord, 3, a_strk.colr, None)
cntr.draw()
#end draw_center
def draw_disk(self):
if self.fcolr == None:
return
self.prepare_to_draw()
#draw the disk
pendown()
color(self.fcolr)
begin_fill()
circle(self.radius)
end_fill()
self.return_home()
#end draw_disk
def draw_border(self):
if self.strk == None:
return
self.prepare_to_draw()
#draw the border
pendown()
color(self.strk.colr)
pensize(self.strk.sz)
circle(self.radius)
self.return_home()
#end draw_border
def draw(self):
self.draw_disk()
self.draw_border()
#end draw
def connect(self, other, strk):
setposition(self.x, self.y)
pensize(strk.sz)
color(strk.colr)
pendown()
setposition(other.x, other.y)
penup()
#end connect
#end class TNTDisk
