Coding Challenge #50.1: Animated Circle Packing - Part 1

The Coding Train
9 Jan 201728:32

Summary

TLDRIn this coding challenge, Dan introduces a creative project using Processing, a Java-based programming environment, to explore animated circle packing. He demonstrates how to create a 'Circle' class and use an ArrayList to manage multiple circles. The challenge involves growing circles that stop expanding upon touching the window edges or overlapping with others. Dan also hints at future enhancements, such as using a source image to guide circle placement for forming letter shapes and dynamically generating letter paths for personalized animations.

Takeaways

  • ๐Ÿ˜€ The video is a coding challenge focused on animated circle packing to form letter forms using the programming environment Processing, which is built on top of Java.
  • ๐Ÿ” The presenter, Dan, plans to release the source code in both Processing Java and JavaScript using the p5.js library for those interested in the project.
  • ๐ŸŽจ The inspiration for the project comes from the work of artist Marius Watts, who is known for engaging computational art involving circle packing.
  • ๐Ÿ“ The fundamental concept of circle packing involves fitting as many circles of varying sizes into a space without overlapping.
  • ๐Ÿ‘พ Dan introduces a 'Circle' class with properties for x and y coordinates for the center and a radius, and methods for displaying and growing the circles.
  • ๐Ÿ› ๏ธ The coding process includes creating a constructor for the circle class, handling edge cases like circles touching the window edges, and managing a list of circles using an ArrayList in Java.
  • ๐Ÿ”„ The video demonstrates the iterative growth of circles and the logic to stop their growth when they reach the window edge or overlap with other circles.
  • ๐ŸŒŸ A key feature implemented is the dynamic creation of new circles at random locations, ensuring they do not overlap with existing circles.
  • ๐Ÿ” Dan discusses the possibility of refining the algorithm to handle pixel width and overlap more gracefully, adjusting the growth rate for better resolution.
  • ๐Ÿ–ผ๏ธ The video concludes with an advanced technique of using a source image to determine the initial placement of circles, specifically using white pixels as starting points.
  • ๐Ÿ”ฎ Future considerations mentioned include dynamically computing letter paths for circle packing, allowing users to input text that is then filled with circles.

Q & A

  • What is the main focus of this coding challenge?

    -The main focus of this coding challenge is to create an animated circle packing algorithm that forms letter shapes.

  • Which programming environment is being used in this challenge?

    -The challenge uses Processing, a programming environment built on top of Java.

  • What is the purpose of the Circle class in this code?

    -The Circle class is used to define the properties and behaviors of a circle, including its x and y coordinates and radius.

  • How does the code ensure circles do not overlap?

    -The code checks the distance between each new circle and existing circles. If the distance is less than the sum of their radii, it stops the new circle from growing.

  • What is the function of the ArrayList 'circles'?

    -The ArrayList 'circles' is used to keep track of all the circles created and manage their growth and display.

  • How does the grow function work?

    -The grow function increases the radius of the circle by one pixel each time it is called, unless the circle touches an edge or another circle.

  • What method is used to detect if a circle has touched an edge of the window?

    -The edges method checks if the circle's x or y coordinate plus its radius is greater than the window width or height, or if it's less than zero.

  • How does the code add new circles to the canvas?

    -The code uses a random location generator to place new circles, ensuring they do not overlap with existing circles by checking their distances.

  • What is the role of the spots ArrayList?

    -The spots ArrayList stores potential starting points for new circles, based on the white pixels in a source image.

  • How does the script handle the completion of the circle packing process?

    -The script includes a counter and an attempt limit to stop the loop if it can't find new spots for circles, printing 'finished' when done.

Outlines

00:00

๐Ÿ“ Introduction to Animated Circle Packing Coding Challenge

In this introductory paragraph, the speaker, Dan, launches into a coding challenge focused on animated circle packing. He plans to build upon a previous challenge and explore the creation of letter forms using animated circles. The coding environment of choice is Processing, which is Java-based, with a JavaScript version using the p5.js library also promised. The concept of circle packing is introduced as an algorithm to fit non-overlapping circles of varying sizes into a space efficiently. The script mentions the work of artist Marius Watts and sets the stage for creating a 'Circle' class with properties for center and radius.

05:03

๐Ÿ” Developing the Circle Class and Growing Function

This paragraph delves into the development of the 'Circle' class, which includes a constructor for creating circle objects with specified x and y coordinates. The speaker discusses the creation of a 'show' function to render the circle using the 'ellipse' function in Processing. Additionally, a 'grow' function is introduced to increment the circle's radius, and an 'edges' function is devised to detect when a circle touches the window's edge, thereby preventing it from growing beyond the boundaries.

10:04

๐Ÿ› ๏ธ Expanding Functionality for Circle Interaction

The speaker continues to expand the circle class by adding functionality to handle multiple circles using an ArrayList. He corrects a mistake with the 'add' method and demonstrates how to iterate over the list of circles to call their 'show' and 'grow' functions. The paragraph also introduces logic to prevent circles from growing if they touch the edges or another circle, ensuring that the circles fill the space without overlapping.

15:05

๐Ÿ”„ Implementing Dynamic Circle Growth and Boundary Conditions

In this segment, the speaker discusses the implementation of dynamic circle growth within the 'draw' loop, adding new circles at random locations with a starting radius of 1. He refines the logic to ensure that circles stop growing when they touch the window edge or another circle. The speaker also introduces a mechanism to limit the number of new circle attempts per frame to prevent the program from getting stuck in an infinite loop.

20:08

๐ŸŽจ Using a Source Image for Circle Placement

The speaker introduces a technique to use a source image to determine the initial placement of circles, allowing for the creation of circle packing that forms specific letter shapes. He describes loading an image and using its pixels to find valid starting points for new circles, where only white pixels in the image are considered as potential circle centers. The speaker also discusses the possibility of using this method for more complex patterns and images.

25:12

๐ŸŒŸ Finalizing the Animated Circle Packing Algorithm

The final paragraph wraps up the coding challenge by refining the circle packing algorithm to use the source image for initial circle placement and ensuring that circles do not overlap and fill the space effectively. The speaker talks about potential future enhancements, such as dynamically computing letter paths for circle packing and exploring color and animation variations. He concludes by encouraging viewers to create something creative with the algorithm and hints at a future challenge involving dynamic letter path computation.

Mindmap

Keywords

๐Ÿ’กCoding Challenge

A coding challenge is an exercise or problem-solving task in computer programming designed to test one's coding skills and creativity. In the context of the video, the host, Dan, introduces a coding challenge focused on creating animated circle packing to form letter forms, demonstrating the process through programming in Processing and JavaScript with p5.js.

๐Ÿ’กProcessing

Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. In the video, Dan uses Processing to build the foundation for the animated circle packing algorithm, showcasing its capabilities for graphics and animation.

๐Ÿ’กCircle Packing

Circle packing involves fitting as many circles of varying sizes into a given space without overlapping. It's a common computational geometry problem. The video's theme revolves around this concept, where Dan explores animated circle packing to create visually appealing letter forms.

๐Ÿ’กp5.js

p5.js is a JavaScript library that simplifies the process of coding for artists, designers, educators, and beginners. It is used in the video to create a version of the animated circle packing that can be run in web browsers, demonstrating the portability of the concept from Processing to the web.

๐Ÿ’กClass

In object-oriented programming, a class is a blueprint for creating objects. In the script, Dan defines a 'Circle' class with properties like x, y for the center and radius. This class is essential for creating and managing circle objects in the coding challenge.

๐Ÿ’กConstructor Function

A constructor function in programming is a special type of function called when an object is created. In the video, Dan writes a constructor function for the 'Circle' class that initializes the circle's position and radius, which is a fundamental part of setting up objects in the challenge.

๐Ÿ’กArrayList

An ArrayList is a data structure that can dynamically resize itself as elements are added or removed. In the script, Dan uses an ArrayList to manage multiple circle objects, which is crucial for the circle packing algorithm where multiple circles are involved.

๐Ÿ’กGrow Function

The grow function is a method within the 'Circle' class that increases the circle's radius by one pixel each time it is called. This function is key to the animation aspect of the challenge, as it allows circles to expand over time until they reach the edges or overlap with other circles.

๐Ÿ’กEdges Function

The edges function is used to determine if a circle has touched the edge of the window or not. It's a boolean function that helps control the growth of the circles in the video, ensuring they do not grow beyond the boundaries of the display area.

๐Ÿ’กOverlap

In the context of the video, overlap refers to the condition where two circles touch or encroach upon each other's space. Detecting overlap is essential for the circle packing algorithm to function correctly, as it prevents circles from growing into one another and ensures efficient space utilization.

๐Ÿ’กPixel

A pixel is the smallest unit of a digital image. In the video, Dan discusses reading the pixels of a source image to determine where to start new circle packing spots, specifically looking for white pixels to initiate the growth of circles, which ties the circle packing to the visual content of the image.

Highlights

Introduction to a coding challenge focused on animated circle packing to form letter forms using Processing.

Use of Processing, a Java-based programming environment, and plans for a JavaScript version using p5.js library.

Concept of circle packing explained as fitting non-overlapping circles of varying sizes within a space.

Creation of a 'Circle' class with properties for center coordinates and radius.

Implementation of a constructor function for the 'Circle' class to initialize circle properties.

Introduction of a 'show' function within the 'Circle' class to draw the circle using Processing's ellipse function.

Utilization of an ArrayList in Java to manage a collection of 'Circle' objects.

Development of a 'grow' function within the 'Circle' class to increment the circle's radius.

Inclusion of an 'edges' function to detect when a circle touches the window's edge, halting its growth.

Algorithm to add new circles at random locations within the window, starting with a small radius.

Introduction of collision detection to prevent new circles from overlapping with existing ones.

Refinement of the algorithm to ensure circles stop growing when they touch another circle or the window edge.

Optimization to limit the number of new circle attempts per frame to avoid infinite loops.

Use of a source image to determine valid starting points for circle packing within the image's white pixels.

Discussion on potential applications of the circle packing algorithm for artistic and creative purposes.

Future plans to explore dynamic computation of letter paths for circle packing without source images.

Conclusion of the coding challenge with an invitation for viewers to share their creative implementations.

Transcripts

play00:00

hello welcome to a coding challenge my

play00:02

name is Dan I know I'm introducing

play00:04

myself at the beginning of this one

play00:05

because anyway but I can because I just

play00:07

did um this is a coding challenge I am

play00:10

going to build off of in some ways a

play00:12

coding challenge that I did previously

play00:14

and I want to look at animated circle

play00:18

packing and I want to look at animated

play00:20

circle packing in the context of forming

play00:23

letter forms look can I do all this in a

play00:26

coding challenge I think that I can I'm

play00:27

going to use processing which is a

play00:29

programming environment built on top of

play00:30

Java

play00:31

I will also hopefully release a

play00:33

JavaScript version of this so you can

play00:35

check this description for the source

play00:36

code after I bake this I'll release the

play00:38

code in both processing Java and also

play00:40

JavaScript using a p5.js library so

play00:42

circle pad King is one of these cut

play00:45

almost computational cliche algorithms

play00:47

in a way there's some images here from

play00:49

an artist named Marius Watts who does

play00:51

lots of beautiful and interesting

play00:52

engaging computational work and has

play00:54

worked with circle packing and the idea

play00:56

of circle packing is fit a lot of

play00:57

circles as many as you can of varying

play00:59

sizes in a small space or a large space

play01:02

and don't have them overlap so in order

play01:05

to do this the first thing that I want

play01:07

to do is I actually want to create a

play01:08

circle class so I'm going to say class

play01:12

circle and what does every circle have

play01:15

in my world every circle has an x and a

play01:19

y storing values for its center and

play01:23

every circle has a radius the distance

play01:26

between its center and the edge and then

play01:29

I'm going to need some other stuff that

play01:32

I just happened but I'm too you know for

play01:33

now I'm just going to put that there so

play01:35

when I make a class which is a template

play01:37

for making objects

play01:38

I need to write a constructor function

play01:40

and my constructor function will take

play01:43

three arguments because whenever and

play01:47

actually actually just two arguments

play01:49

let's just give it an X and a Y because

play01:51

whenever I create a circle I want to say

play01:53

create a circle at this look at a

play01:55

particular location and then X gets that

play01:59

particular value and Y gets that

play02:00

particular value now I'm doing something

play02:01

kind of goofy here which is that the

play02:04

arguments of the constructor I'm using

play02:06

I've been using the same variable name

play02:08

as the properties of the object itself

play02:09

but just with the underscore afterwards

play02:11

and what that does it allows

play02:13

to differentiate these temporary our

play02:16

constructor arguments to fill the actual

play02:18

variables of the object but you know

play02:20

it's kind of a confusing convention but

play02:21

it's one that I'm kind of used to so I'm

play02:23

going to use that here so what this

play02:24

means is now I can say something like

play02:27

Circle C and I can say I'm going to in

play02:31

processing I'm going to say sighs let's

play02:33

just say a 640 360 and in drama say

play02:38

background zero and then I'm going to

play02:40

say in setup C equals a new circle 200

play02:45

200 and I can run this program and cut

play02:48

off I have a circle where is it I don't

play02:53

see it because what do I want to do I

play02:55

want to write up some functionality in

play02:57

the circle object itself

play02:58

hmm I'm going to call it show and then

play03:03

we use the ellipse function which is a

play03:05

function processing that draws a ellipse

play03:07

and an ellipse with a width and height

play03:12

the diameter across the horizontal and

play03:15

vertical axes of the same amount is a

play03:17

circle and what should that diameter be

play03:19

it's the radius times two so now if I

play03:22

said something like C display Oh

play03:27

any calls display I called it show ah ah

play03:31

where's that circle hmm I don't know why

play03:33

I don't see that circle interesting

play03:36

problem here ah why well R has what's

play03:39

the value of R the value of R is zero

play03:41

so let's give the value of our 50 right

play03:44

now and let's also be a little more

play03:46

thoughtful here and say stroke 255 and

play03:49

no fill so now I see there's my circle

play03:52

now you I don't know how well you can

play03:54

see this I can zoom in you can see it

play03:55

but I'm going to just increase the

play03:57

thickness of the line just because I

play03:59

don't know in terms of broadcasting this

play04:00

on YouTube and all that sort of stuff so

play04:02

you can see there's the nice beautiful

play04:04

lovely circle oh I love that circle that

play04:06

circle is my friend like I got to move

play04:09

on okay so what do I want to do now what

play04:12

I want to do now is not just have one

play04:13

circle

play04:14

I need a system where I'm going to keep

play04:15

track of many circles and a way I can do

play04:18

that in Java is using something called

play04:20

an ArrayList now oops

play04:22

I this syntax is a little bit goofy an

play04:24

ArrayList is an object that creeps

play04:26

that keeps a list of objects and I can

play04:29

say in advance I want a list of objects

play04:31

of type circle and that's what I'm doing

play04:33

right here and that list is called

play04:34

circles so now instead of having a

play04:36

single variable I can say circles dot

play04:41

push new circle and then I can say a for

play04:48

every and this is a nice little enhanced

play04:51

loop that you can write in Java

play04:54

these days with array lists you can say

play04:57

for I'm standing in front of this for

play04:59

every circle see in the ArrayList

play05:03

circles operate call the show function

play05:05

on those circle seats so for every

play05:06

circle call show okay here we go

play05:09

oh snap push it's not push it all this

play05:12

is me I'm the kind of person who

play05:14

programs in too many languages these

play05:15

days it's add push would be a function

play05:18

in JavaScript to push something into an

play05:19

array in Java using ArrayList the

play05:23

function is add so now if I do this we

play05:25

can see ah what happened I got a

play05:27

nullpointerexception see that null

play05:29

pointer exception that's the sound that

play05:33

goes the processing should play whatever

play05:35

you got to know upon your exception I

play05:36

forgot to actually create the ArrayList

play05:38

so I said oh I want to have an ArrayList

play05:40

but it's actually just null at this

play05:42

point so I need to actually take an

play05:44

additional step and say circles equals

play05:46

and new it seems rather redundant but

play05:48

this is the world of Java many people

play05:50

don't like Java but you know what Java

play05:52

is also my friend and I like Java okay

play05:55

so new ArrayList that means are

play05:59

something wrong with me then so be it

play06:00

okay

play06:01

so there you can see now I have this one

play06:03

circles oh boy

play06:05

what about like five minutes into this

play06:07

video and all I have a circle on the

play06:08

screen I hope you're still with me

play06:10

because this this will get more

play06:11

interesting okay so now I want to do I

play06:13

want to do a few different things one

play06:18

thing I want to do is I want to have a

play06:21

function in the circle called grow and

play06:24

what I'm going to do is I'm going to say

play06:25

R equals R plus 1 so I want the radius

play06:29

to increase by one pixel whenever I call

play06:33

the grow function and I'm going to say

play06:35

now every circle see grow

play06:38

you can see that circle is growing now

play06:41

the other thing I want to do is I want

play06:42

to stop the circle I want to have a way

play06:44

of stopping the circle from growing so

play06:47

one thing I should do is I'm going to

play06:49

write a function called edges to see has

play06:53

the circle touched an edge of the window

play06:58

and actually if it has I should maybe I

play07:04

should return a boolean true or false we

play07:08

could do this in a bunch of different

play07:09

ways but let's start with this so how do

play07:11

I know if a circle and let me just come

play07:15

over to the whiteboard here

play07:16

look there's a happy party time so right

play07:20

if this is a the window and this is a

play07:25

circle if the X plus the radius is

play07:32

greater than the width of the window or

play07:36

if X minus the radius is less than zero

play07:39

then it's kind of hit that edge so let's

play07:42

come back and add that if what I say X

play07:45

plus R is greater than width or X minus

play07:49

R is less than 0 or y plus R is greater

play07:53

than height or Y minus R is less than 0

play07:58

that should do it and actually I could

play07:59

just say return that this is one of

play08:03

these shorthand things that I never use

play08:04

and people always complain in the

play08:05

comments like you're going to just

play08:06

return the result of that so I'm

play08:09

returning the result of this or that or

play08:11

this or that so if any of those are true

play08:13

I'm going to return true so what I'm

play08:15

going to do here is I'm going to say I'm

play08:17

also going to add a variable I'm going

play08:19

to call it growing I'm going to set it

play08:21

equal to false no true and I'm going to

play08:27

say if growing increase the radius by

play08:32

one okay this is good this is good this

play08:35

is good now I'm going to write a

play08:37

function let's uh there's a bunch of

play08:41

different ways I could do this but let's

play08:43

do it actually out here I don't know if

play08:45

I love this but I'm gonna say if C dot

play08:46

edges see growing equals false so this

play08:50

should stop

play08:52

and what did I find missing a semicolon

play08:55

here because I have this like

play08:57

ridiculously long line of code that is

play08:59

very unwieldy to type but it's come back

play09:02

now this should now grow the circle

play09:06

until it reaches the edge and you can

play09:08

see why did it stop it touched the

play09:10

bottom edge down here so this is good I

play09:12

now have a circle that can grow and a

play09:15

circle that can stop growing and what I

play09:18

want to do is I want to add new circles

play09:23

every time through draw and I want to

play09:27

give it a random location so I'm going

play09:33

to create a new circle at a random

play09:36

location and I want the circles to all

play09:39

start not with a radius of 50 but a

play09:42

radius of 1 and watch what happens out

play09:44

so I have all these circles ooh that's

play09:46

kind of actually just nice on its own

play09:48

they're all growing and they're stopping

play09:50

as they reach the edge but let's watch

play09:52

that again let me slow down the

play09:55

framerate what I want to see happen is I

play10:00

want all these circles to grow but it

play10:02

also want them to stop growing if they

play10:03

touch another circle and I don't want to

play10:07

be able to create a circle inside of

play10:09

another circle because I want to have

play10:10

these circles kind of grow to fill as

play10:12

much space as possible okay so how do I

play10:18

do this so the first thing that I need

play10:21

to do is um I don't know what is the

play10:26

first thing that I need to do do you

play10:27

don't want to do I'm going to I'm going

play10:32

to I'm going to create a function called

play10:33

new circle and I'm because I have a

play10:39

feeling this might get a little bit

play10:41

complicated

play10:46

so what I'm going to try to do now here

play10:49

is what I'm going to create a random XY

play10:55

then what I want to do is say for every

play10:59

circle in all the circles what I want to

play11:02

do is determine when I click this XY is

play11:05

it a point that's outside of circles

play11:08

right I don't want to by accident these

play11:13

are my circles if I pick that point it's

play11:16

no good if I pick this point we're good

play11:19

shape so what I need to do is I need to

play11:22

check what are we twin time works okay I

play11:24

need to say what is the distance between

play11:29

this X Y and that circles X Y if the

play11:33

distance is less than that circles

play11:35

radius then it is inside the circle so

play11:40

let's assume that this is a valid circle

play11:46

if I look at all the existing circles

play11:48

and it's inside one of them it's no

play11:52

longer a valid circle and I could also

play11:54

be done with that loop if it's not valid

play11:57

I could just check one of them now what

play11:59

I want to do is if it's valid I want to

play12:05

add it to the ArrayList so I only want

play12:08

to add circles that are valid and you

play12:10

can see now if I run this again you know

play12:11

it's everything still overlapping but

play12:13

technically speaking it's not you can

play12:15

see it's not adding any circles anymore

play12:17

until it finds a spot that's not inside

play12:19

another circle I like this but actually

play12:22

I think what might make more sense here

play12:23

is to return a return a new circle

play12:32

otherwise return null so and then this

play12:37

is going to be a function that returns a

play12:40

circle so what I want to do is I want to

play12:42

say like this is this function is going

play12:44

to attempt to find a new circle and if

play12:49

it does here it is if I can't find one

play12:51

it's no so what I'm going to do now is

play12:55

I'm going to say

play12:58

Circle C equals new circle if C is not

play13:03

equal to no circles add C so now I have

play13:08

oh and you know what I'm going to just

play13:10

call this circle I'll call it new circle

play13:15

oh that's a little bit awkward that I

play13:21

have new circle in the new circle

play13:22

function let's just call it new C it

play13:26

works but you know when we translate

play13:28

this job okay so this is working this is

play13:29

exactly what I just had it's trying to

play13:31

make these circles now I need to add the

play13:33

part where if it touches any other

play13:36

circle it also stops growing so for

play13:42

every circle if it's touching an edge it

play13:45

stops growing so first of all I only

play13:49

want to do this if the circle is growing

play13:51

if the circle is growing check first if

play13:56

it's touching an edge otherwise now I

play14:00

got a look at all the other circles

play14:03

again and I can say boolean overlapping

play14:10

I can assume that it's not overlapping

play14:13

another circle I can assume that it's

play14:15

not overlapping another circle now what

play14:17

I can do is I could say the distance

play14:19

between this particular current circle

play14:23

and the other circle

play14:28

I need that distance if that distance is

play14:33

less than what if that distance is less

play14:37

than this circles radius plus the other

play14:40

my scroll went under control plus the

play14:43

other scrolls the other circles radius

play14:45

then overlapping is true why is that

play14:49

come over here with me please because

play14:53

here's a circle here's a circle here's

play14:57

one radius here's another radius here's

play15:00

the distance between them you can see

play15:03

that if the distance is longer than the

play15:04

sum of the two radii then they're not

play15:06

overlapping if that distance is shorter

play15:10

than

play15:10

adding the two radii together they are

play15:12

they are overlapping so now all I have

play15:16

to do is actually don't need this

play15:18

overlapping variable I just realized

play15:20

because the point of this is I can say

play15:22

if it's overlapping anything tell it to

play15:25

stop growing and break so this is now my

play15:28

algorithm right if a circle is growing

play15:30

there are two reasons why I might stop

play15:32

it from growing one is it might be

play15:34

touching the edge otherwise it might be

play15:36

overlapping with another circle so if

play15:38

either one it should stop so now let's

play15:40

run this and see what we get

play15:41

mmm seems to me that these circles

play15:46

aren't growing ah what did I oh my

play15:48

goodness I like to ring the bell when I

play15:50

discover a really ridiculous bug look at

play15:53

this

play15:54

for every circle see check every other

play15:57

circle called other well guess what see

play16:01

is going to check every other circle and

play16:03

you let's see is it's one of those every

play16:05

other circles so I need to also make

play16:07

sure as long as C does not C does not

play16:13

equal other as long as it's not the same

play16:15

object then I can do this test there we

play16:21

go so now we can see here we go I'm just

play16:24

filling this space and you know it made

play16:27

some big circles at the beginning now

play16:29

it's making some smaller ones because it

play16:30

keeps trying to find more spots where it

play16:32

could possibly fit a circle and we can

play16:34

let this run and eventually it's going

play16:36

to do it now we could say we could we

play16:38

could do some stuff to make this a

play16:39

little fancier so but here's the thing

play16:43

this is a pause point me to do something

play16:45

more of this in a second because I want

play16:46

these circles to form a letter or a

play16:48

shape but what I could do here is I

play16:53

could start to think about well what are

play16:55

some ways you know should I only start

play16:57

the circles where the user clicks the

play16:58

mouse should I start the circles along a

play17:00

sine wave pattern should I start the

play17:02

circles like you know randomly what

play17:05

types of what kind of color what type of

play17:07

rules should I apply to when and where I

play17:09

start the circles and you could probably

play17:10

create more geometric kinds of

play17:13

interesting pattern so I would go with

play17:16

this go forth and make your rainbow

play17:19

circle packing magic and maybe fingers

play17:21

what other

play17:22

you could animate the circles one of

play17:23

things could do besides just drawing

play17:24

circles this idea of filling a space but

play17:26

one thing I do want to do a couple of

play17:28

things here is you can see like that

play17:29

overlapping is a little bit awkward so I

play17:32

think I probably could kind of clean

play17:34

that up by saying like because the the

play17:37

pixel width of those circles is too so

play17:39

you know I could probably oh and the

play17:42

other thing is Oh a - - I mean so you

play17:47

can see this is making it a little bit

play17:49

better so the circle is actually kind of

play17:50

like stop and abut each other a little

play17:53

bit sort of depends on the size but so

play17:55

we could refine that and you know that

play17:57

also like I could probably have the

play18:01

circle grow a little bit slower and then

play18:04

it gives it a little bit more resolution

play18:07

in terms of how close it gets but these

play18:09

are these are smaller points the point

play18:11

that I want to actually do is I think

play18:13

that I think they'll be useful to add is

play18:15

I want to let's say I want to I want to

play18:19

make sure I add 10 new circles every

play18:21

frame so I'm going to say count equals

play18:25

zero

play18:26

and while count is less than total

play18:30

whoops create a new circle if new circle

play18:37

is not no then count goes up and then

play18:44

we're done so this would guarantee that

play18:47

it's going to like keep trying to find

play18:49

more circles every frame up to ten the

play18:52

problem with that you can see it's

play18:53

filling it much more quickly the

play18:55

probably this is it's going to get stuck

play18:56

at some point an infinite loop is it

play18:58

simple it's not going to be able to find

play19:00

ten new spots anymore

play19:01

because the whole screen is going to be

play19:03

full so what I think would make sense to

play19:07

do in that case would be to have some

play19:09

sort of like I'm going to also have a

play19:12

variable called attempts I'm going to

play19:15

say a temp attempts plus plus and if

play19:19

attempts is greater than 1,000 then just

play19:23

let's be done you know I can even say

play19:26

like no loop so I can completely stop

play19:29

the program from running and I can say

play19:33

you know print line finished

play19:36

so this is just sort of like useful say

play19:39

like oh if it took a thousand tries and

play19:41

couldn't find any spots for the circles

play19:43

then it should be done we can kind of

play19:45

zoom in and see what this is doing it's

play19:47

still finding more spots at some point

play19:49

it's going to stop finding spots I'll

play19:51

let this run for a little bit okay

play19:53

so while this is running I'm pretty sure

play19:56

that I did this correctly

play19:57

he's like waiting for it to say finish

play19:59

down here in the console but while this

play20:02

is running let's go and let's let's add

play20:04

one more thing to this Wow I guess it's

play20:08

still just finding spots it's not done

play20:10

there's just so many spots left okay

play20:13

it's really filling it in there the

play20:15

other thing I could do is I could start

play20:16

the circles with like a little bit of a

play20:19

radius like not one but like two anyway

play20:22

but okay all that's neither here and

play20:25

there you can tweak them you can tweak

play20:26

the stuff yourself but I assume it will

play20:29

get to finish at some point what I want

play20:30

to do right now is I have on my desktop

play20:33

here is I have this image and what one

play20:36

trick that I can do so there's a couple

play20:37

different ways I could decide there's a

play20:39

lot of different ways I could decide

play20:40

where to seed the circle points one way

play20:45

that I could do that is like it's a I'm

play20:47

going to have a source image I'm going

play20:48

to read the pixels of that source image

play20:50

and only ever going to be allowed to

play20:52

start circles where there's a white

play20:54

pixel in that source image and that's

play20:56

what I'm going to do right now to make

play20:58

this 2017 message replace it with your

play21:01

own message later okay so let's see is

play21:06

it still going oh I stopped it

play21:08

okay so let's let's do this and I'm

play21:13

going to say this as circle packing

play21:14

animated text and I'm going to ups I'm

play21:18

going to go to the sketch folder I'm

play21:21

going to create a folder called data and

play21:23

I put this 2017 PNG in there what is the

play21:27

size of that image is important the size

play21:30

of that image is 900 by 400 okay so

play21:33

let's make a the window also 900 by 400

play21:38

let's say a P P image image if I'm going

play21:44

to say image equals load image 2017 dot

play21:48

PNG

play21:48

and then I'm going to just say

play21:51

background just to make sure that image

play21:53

is there we can see the images there now

play21:55

2017 I'm drawing circles on top of it

play21:58

yay okay so now what do I do here what I

play22:01

want to do is I need to say image load

play22:04

pixels because what am I going to do I'm

play22:07

going to read the pixels of that image

play22:10

then I'm going to say 4 into X equals 0

play22:13

X is less than image dot with X plus

play22:15

plus 4 int y equals 0 Y is less than

play22:20

image dot height y plus plus what I want

play22:25

to do here in setup is I want to have a

play22:28

nested loop a loop that looks at every

play22:30

single pixel I want to know is the pixel

play22:32

white or black so how do I look at the

play22:35

pixel well the pixels by the way even

play22:37

though I'm thinking of them as XY

play22:39

locations they are just a linear list 0

play22:42

1 2 3 4 5 6 7 8 9 all the way up to 900

play22:45

times 400 minus 1 but and I have some

play22:48

videos that cover how this sort of pixel

play22:50

stuff works but I can find the index

play22:52

into that pixel array the

play22:54

one-dimensional pixel array by saying

play22:56

the X location plus the Y location

play22:58

multiplied by the images with you can

play23:01

think about that makes sense if the

play23:02

image was 5 pixels wide it's going to be

play23:04

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 right

play23:09

0 5 10 so you can sort of see how that

play23:11

works it's the row plus no it's the

play23:15

column plus what row it is times the

play23:17

width of those columns okay so now I'm

play23:19

going to say the color is image pixels

play23:24

index and then the brightness I'm just

play23:30

gonna look at the brightness of that

play23:31

color if the brightness of that color is

play23:34

greater than I don't know 1 then I've

play23:38

got a white pixel right I could say if

play23:40

it's greater than 254 probably but in

play23:42

this case this was kind of a really

play23:43

simple test if it's greater than what am

play23:45

i standing in front of my code all this

play23:47

time oh okay if it's greater than 1 oh

play23:51

boy this video hope it's not totally

play23:52

ruined if it's greater than 1 then I

play23:54

found a bright pixel so what do I need I

play23:57

want to have an ArrayList

play24:02

of spots and those spots I want to have

play24:07

a list of possible spots that the that

play24:11

the circle packing algorithm can pick

play24:12

from and each one of those spots is

play24:14

essentially a vector processing has a

play24:16

class called P vector that stores an x

play24:18

and a y so what I'm going to do is I'm

play24:20

going to say right before I do this I'm

play24:23

going to say spots equals a new array

play24:25

list full of P vector objects and if the

play24:29

brightness is good spots add a new P

play24:33

vector at that X&Y location

play24:36

todo spots out add a P vector at that

play24:39

X&Y location so now if I run this I'm

play24:41

just going to say print line spots dot

play24:44

size OOP Y may not have been initialized

play24:48

whoops that should be a zero so great so

play24:52

this looks like it worked it found

play24:54

77,000 759 possible spots so now what

play25:02

can I do here here instead of creating a

play25:05

random x and a y anywhere in the window

play25:07

what I want to do is say I want a random

play25:11

number that is between 0 and the length

play25:14

of that spots ArrayList so the random

play25:18

function gives me a random number

play25:19

between 0 and the length of the list and

play25:21

I want to convert that random number

play25:22

into an integer because I want to use

play25:24

that too as the index to get me a

play25:32

particular spot out of that ArrayList

play25:34

and now I can say X is spot that x and y

play25:41

is spot dot Y and now if I lo what did I

play25:44

miss

play25:45

I guess I just need one more close

play25:47

parenthesis now if I run this

play25:49

oh whoops I ruined it by drawing the

play25:52

image so let's stop drawing the image

play25:55

and we can see there we now have our

play25:58

circle packing algorithm but it is only

play26:00

picking as original spots spots that are

play26:03

that are available in the actual as part

play26:08

of those white pixels from that source

play26:09

image so you can see we have this nice

play26:11

circle packing I'm still finding spots

play26:14

because hold on a sec let's let's let's

play26:18

be a little bit more let's say that it

play26:21

has to be the valid spot has to be um it

play26:28

doesn't have to just be the distance

play26:30

between it and the center has to be less

play26:33

than the radius plus like a little bit

play26:35

of buffer zone like two pixel buffer

play26:37

zone so that I think will cause it to

play26:40

finish you can see there's lots of ways

play26:42

I could futz with like what it's

play26:44

actually going to like what if I just

play26:45

want one circle each frame it's going to

play26:48

pick circles much less frequently and so

play26:51

the circles are going to have a longer

play26:53

time to grow and again you can start to

play26:54

think about color here how you're

play26:56

picking the circles when you're picking

play26:58

the circles you know not being able to

play26:59

pick the same point twice will probably

play27:01

make a lot of sense and here we go

play27:04

animated circle packing making

play27:07

letterforms now this is the end of this

play27:10

video coding challenge thank you very

play27:11

much I hope you make something creative

play27:12

with it you think of a message you think

play27:14

of some other type of pattern you think

play27:16

about color animation in a different way

play27:17

share it with me all that stuff but I do

play27:20

want to say is I am using a source image

play27:26

to find the paths of these letters which

play27:29

is great in the sense that you could use

play27:30

a source image not just to do letters or

play27:33

numbers you could use you know painting

play27:34

and then do circle packing to like fill

play27:36

a space with colors from the painting

play27:38

there's also two possibilities there but

play27:39

one thing I do want to look at and

play27:41

perhaps in a future coding challenge may

play27:44

give you in the next one is how to

play27:46

actually compute these paths of how to

play27:49

actually get these paths of letters

play27:51

dynamically in a program so the user

play27:53

could type in the word hello and then

play27:55

suddenly you could fill a circle packing

play27:57

space or whatever with that with that

play27:59

particular word I'm going to look at

play28:00

that in javascript in the browser

play28:01

because p5 has some nice functionality

play28:03

for that you can do in processing also

play28:05

with the library called jammer tip which

play28:07

I'll try to link in this video's

play28:08

description

play28:08

but that's how you might look for okay

play28:10

so thanks for tuning in to this coding

play28:12

challenge i back it's 2017 i've made a

play28:14

coding challenge i think things are

play28:15

still ok people will hopefully watch

play28:17

this hope we make something with it and

play28:18

talk to you soon goodbye

play28:21

[Music]

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
Coding ChallengeCircle PackingAnimated ArtProcessing JavaJavaScriptp5.jsAlgorithmsGraphic DesignInteractive ArtCreative CodingPixel Analysis