Writing code: experimentation is the best part

random_by_jmummeyI recently wrote a desktop application, a small piece required that I draw a simple rectangle to the screen. When I drew the rectangle I made an error, I put the x-coordinate in as the width and the width as the x-coordinate. The result was pretty but not what I wanted so I fixed it and moved on. But I came back to it later that night and started fiddling with the code to generate more unexpected graphics. In the end I had written a simple method in the C# language where I feed it random x and y coordinates as well as random height and width sizes and random shapes to draw on the screen. I liked the images so much (only because some remind me of Star Trek) that I picked out what I consider to be the coolest 60 images and published them in my first artistic coloring book called “Random”. The book size is 8 ½ x 11 and each image is on the right with a blank page on the back so the images can easily be colored, removed and framed without disrupting the other images. Check it out and color all of your boring processes away: https://www.amazon.com/Random-Coloring-computer-generated-coordinates/dp/1539987582

If you are interested in learning how to write code, the instructions below will help you get started, these are the steps I went through to create some of the artwork in the book Random that I mention above, enjoy and code on:

Language used:  C#
Type of application written:  console application running on my Windows desktop PC (no server needed)
Developer tools used:  Visual Studio, running on my Windows desktop PC (this is available to you for free): https://www.visualstudio.com/free-developer-offers/

Steps performed for drawing ellipses:

  1. Install Visual Studio from the link provided above
  2. Reboot
  3. Open Visual Studio
  4. Select File > New > Project > Templates > Visual C# > Windows > Classic Desktop > Console Application
  5. Name the application and choose a location to store it > select OK
  6. The “Program.cs” file should have opened by default for you. It will look something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)//main method begins
{

}//main method ends

}

}

  1. Look in the Solution Explorer (View > Solution Explorer) and add a reference to a library for drawing:
  2. Click once on “References”, right click and select “Add Reference” and find “System.Drawing”, place a checkmark beside “System.Drawing” and select OK
  3. Towards the top of “Program.cs” add this: usingDrawing; and also add using System.Drawing.Imaging;
  4. Inside the Main method, add the following for loop (the purpose of this for loop is make many images. If you set this for loop to run 40 times then that means 40 images will be created and stored in the Temp directory on your c drive.  If you set this for loop to run 10 times then only 10 images will be created and stored in the temp directory on your c drive:

for (int t = 0; t < 40; t++)
{

makeSweetImages3();   //call method repeatedly to create multiple images

}

 

  1. Under the Main method, add the following method, this method does all of the work (declares random coordinates, draws the image and stores the image):

static private void makeSweetImages3()
{

DateTime localDate = DateTime.Now; //we use the milliseconds to uniquely name each image
Random rnd = new Random();
Bitmap bitmap = new Bitmap(2552, 3302);//width, height
bitmap.SetResolution(300, 300);

// Draw graphic
int z = rnd.Next(1, 2550);//to represent width of item being drawn keep btw these values
int b = rnd.Next(1, 3300);//to represent height of item being drawn keep btw these values
int k = rnd.Next(0, 2550 – z);//to represent item on the x axis keep between these values
int m = rnd.Next(0, 3300 – b);//to represent item on the y axis

using (Graphics gr = Graphics.FromImage(bitmap))
{
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//type of line

for (int p = 0; p < 10; p++)//number of ellipses being drawn for this one image
{
System.Drawing.Rectangle rect5 = new System.Drawing.Rectangle(k, m, z, b);
// x, y, width, height, stage is set for graphic, when you draw an ellipse you
//have to define its outer rectangle first then draw the ellipse within or on
//said rectangle
using (System.Drawing.Pen thick_pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2))
{
gr.DrawEllipse(thick_pen, rect5);//draw graphic
}
z = rnd.Next(1, 2550);//width
b = rnd.Next(1, 3300);//height
k = rnd.Next(0, 2550 – z);//x
m = rnd.Next(0, 3300 – b);//y

}
}

bitmap.Save(@”C:\Temp\” + localDate.ToString(“yyyyMMdd-HHmmssfff”) + “.png”, ImageFormat.Png);
//save image to Temp directory on c drive
}

  1. Run the console application by selecting Debug > Start without Debugging (you will notice a black console window will appear, once the console window displays “Press any key to continue…” you will know all drawings are complete, press any key to close the console window)
  2. Navigate to C:\Temp on your computer and view the beautiful images you just created
Advertisements

Technology Dreaming: Singing to the Rockefeller Center Christmas Tree

I do not sing.  I somehow just know that, if I ever attempted such a thing, a horrid aftermath of: babies crying, adults screaming while holding their ears, buildings crumbling, ground shaking and meteor strikes would ensue.  I am so confident of this in fact that I do not even try to sing when I am alone, at night, driving in my car.  It is too dangerous.  I know the sound that would emanate from my mouth would be bad enough to destroy the universe as we know it.

Walking around without the gift of song, for me, is OK.  I’ve never had a desire to sing so the world has been perfectly safe and I have been quite happy listening to others sing.  All of this changed however a few years ago when I was in New York City with my husband.  He had purchased the best Christmas gift for me:  a ticket for an afternoon of ice skating at Rockefeller Center with a small group of other skaters and Kristi Yamaguchi.  The ice skating event in itself was an unforgettable treat.  But the icing on the cake was seeing the Rockefeller Center Christmas tree, in person, for the first time.  WOW!  I saw it at night and I remember laughing to myself because for some reason my first impulse was to sing – definitely a feeling I had never had before.  Luckily for the universe, I did not sing.  But I do hope the powers that be at Rockefeller Center will place a live microphone near the tree this year for anyone that happens by and has the same powerful impulse to sing that I had.

Granted, this “technology dreaming idea” comes with substantial risk of one or more of the following occurring:

  • The microphone and all other necessary equipment are stolen and pawned within the first 20 minutes by a moody tall man wearing no shirt.
  • An over-the-top political activist hogs the microphone all day long so he or she can scream about all of the injustices of the world.
  • Someone who sings so badly that the sound halts traffic and causes car horns to stop functioning steps up to the microphone and belts out 49 songs before someone wearing earplugs is finally able to take them down with a potato sack over the head and a tackle.
  • An angry city puppy gnaws through the electronics, leaving frayed wires dangling in puddles of freshly melted snow for unsuspecting participants to get the shock of a lifetime.

I think the risk is worth it for those few who will show up and sing something that is as beautiful as the tree:)

Create a free website or blog at WordPress.com.

Up ↑