0

I am writing the method to draw a full circle in a numpy array of pixels, and I just don't understand why I am getting a TypeError. I apologize in advanced - I am a new Python programmer who recently switched from Java and I am still having trouble with all of this "lack-of-defining-variable-data-type-ing".

class Image:

    def drawCircle(self, centerX, centerY, radius):
        sin45 = 0.70710678118                                               
        distance = radius/(2*sin45)
        for i in range(radius,distance,-1.0): ####This is the error line####
            j = math.sqrt(r*r - i*i)
            for k in range(-j, j, 1):
                self.writePixel(self.centerX - k, self.enterY + i)
                self.writePixel(self.centerX - k, self.enterY - i)
                self.writePixel(self.centerX + i, self.enterY + i)
                self.writePixel(self.centerX - i, self.enterY - i)

'''Testing the code'''
obj = Image()
obj.drawCircle(35.0, 35.0, 35.0)

This code may be filled with other errors, but as of right now I am stumped on "TypeError: 'float' object cannot be interpreted as an integer". Also, for the sake of saving space I have left the definition for writePixel() out, but just assume that it works. Thanks!

1
  • range cannot accept float, radius and distance might be floats Commented Jul 19, 2019 at 19:15

3 Answers 3

2

Change:

for i in range(radius,distance,-1.0):

To:

for i in range(int(radius),int(distance),int(-1.0)):
#or
for i in range(int(radius),int(distance),-1):

It should be noted that this will round those numbers down so if you need the precision of a decimal you should consider using a linspace from numpy instead of a range.

Also this will only fix the TypeError, not saying this will fix other issues

Sign up to request clarification or add additional context in comments.

1 Comment

This is almost certainly what's causing the error. Worth noting, no need to bring numpy into it - if you need to round to the nearest whole number (and not just strictly down) you can probably just use the built-in round() function instead of int()
0

The third parameter in range function should be integer not a float. I guess that is causing issue. Please try using -1 instead of -1.0 , also convert radius and distance to int.

Comments

-1

In your code your are using variable r without defining it.

Try using int(math.sqrt())

2 Comments

That wouldn't be a TypeError.
Also check for math.sqrt statement it will return float so use int(math.sqrt())

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.