Saturday, January 31, 2015

Plotting Points On A Circle

I once developed a small countdown timer application that used a pie-chart to display the remaining time.  This involved some high school math to be able to plot a line from the center of the circle and plot a radius to specified point on the line.  The same code would also be helpful for developing an application with a compass.

w = width of the space where the circle is bounded
h = height of the space where the circle is bounded
d = diameter (the shorter of w or h)
r = radius = d/2 = SQRT((x2-x1)^2 + (y2-y1)^2)
x1,y1 = circle center
x2,y2 = a point on the circumference
a = angle (from 12:00)
b = modified angle to convert from degrees to radians, reverse direction of unit circle, and start at 12:00 instead of 3:00.
t = total number of segments
s = segment number
a = (360/t) * s = 360 * (s/t)
b = (270 - a) * (Pi / 180)
x2 = x1 + (r * cos(b))
y2 = y1 + (r * sin(b))
x1 = w/2
y1 = h/2

For a space that  has a height of 400 and a width of 648
h = 400, w = 648
d = 400
r = 400/2 = 200
x1 = 648/2 = 324
y1 = 400/2 = 200

In the case of a clock, there are 60 seconds and thus 60 total segments
t = 60

Each second is 1 segment and thus 20 seconds is segment number 20
s = 20
a = (360/60) * 20 = 120
b =  (270 - 120) * (Pi / 180) = 149.9825

x2 = 324 + (200 * cos(149.9825)) = 324 + (200 * -0.5) = 324 + (-100) = 224
y2 = 200 + (200 * sin(149.9825)) = 200 + (200 * 0.866) = 200 + 173.21 = 373.21

In C#.NET, it looks like this:

  int radius = 0;
  double angle = 0;
  lineShape1.X1 = ClientSize.Width / 2;
  lineShape1.Y1 = ClientSize.Height / 2;
  if (lineShape1.X1 < lineShape1.Y1 ) {
      radius = lineShape1.X1;
  }
  else {
      radius = lineShape1.Y1;
  }
  angle =  Convert.ToDouble(360 *
              (Convert.ToDouble(TotalRemaining)
             / Convert.ToDouble(TotalStart)));
  angle = (270 - angle) * (Math.PI / 180);
  lineShape1.X2 = Convert.ToInt32(lineShape1.X1 
                   + (radius * Math.Cos(angle)));
  lineShape1.Y2 = Convert.ToInt32(lineShape1.Y1 
                   + (radius * Math.Sin(angle)));

download exe

download source code



Sunday, January 18, 2015

Step Away From The Ledge

Today I saw a video online where Cenk Uygur was responding to the recent attacks in Paris at the offices of the Charlie Hebdo (Jan 2015).  Cenk described himself as agnostic and he made some good points I would like to recap and respond to.

He asks if God rules the universe, then how likely is he to look down at Earth and respond "Oh no, someone has drawn a negative cartoon about me and my prophets.  I don't know what to do about that.  I sure hope someone defends my honor and gets revenge."

Cenk went on to say that Islam (and other religions) teach that disrespect and/or denial of God and his prophets can lead to eternal pain, suffering, and damnation.  If that is true, then why do offenders need to be killed or punished now?  Isn't it enough that they will suffer for all of eternity?   Compared to that, torture or murder is a walk in the park.

He said that his god is science.  One of the prophets of science is gravity.  The prophet Gravity is not tolerant of disobedience.  If someone where to disrespect Gravity and step off the ledge of a great cliff, there would would be no need to for anyone to defend the honor of Gravity.  No one would need to seek revenge for Gravity.  Upon stepping off the ledge, Gravity would serve his justice swiftly and severely.

Although Cenk may be agnostic, I could not agree with him more.  God does not need me to defend him.  If you deny or dishonor him, that is between you and him.  Those who do not wish to spend eternity with him can spend eternity without him.  In my view, those that reject, ignore, or insult God (however you choose to define that) are indeed headed for a ledge.  I am not injured by such actions.  There is no harm I could do that would be greater than what happens once they leave the ledge.  On the contrary, my compassion for them would lead me to draw them away from the ledge.  I would hope to convince them to stay on solid ground.  I would tell them in caring and compassionate terms about the dangers of the ledge, the wisdom of keeping their distance from it, and the safety and peace that could be found if they would just turn and walk the other way.  The decision would be theirs to make.  I would not (could not) force them to choose the way that I have chosen.  

In short, I would lovingly plead with them to please step away from the ledge.

Saturday, January 10, 2015

Franklin's Whistle

I recently finished listening to Frank Woodworth Pine's edition of Franklin's Autobiography.  I thoroughly enjoyed this book. Franklin seems to have something to say about just about everything.  His "up by his own boot straps" account of his life is fascinating.  The book covers his involvement with writing, printing, politics, government, military campaigns, and science...just to name a few.  I highly recommend it to anyone.  Here are just two sources for the book:
https://librivox.org/the-autobigraphy-of-benjamin-franklin-ed-by-frank-woodworth-pine/
https://books.google.com/books?id=bJIEAAAAYAAJ

At the end of the book, Pine included one of Franklin's letters to Madame Brillon.  An extract from this letter is below which is a good reminder on judgement, financially and otherwise.

Enjoy.

The Whistle
by Benjamin Franklin

When I was a child of seven years old, my friends, on a holiday, filled my pocket with coppers. I went directly to a shop where they sold toys for children; and being charmed with the sound of a whistle, that I met by the way in the hands of another boy, I voluntarily offered and gave all my money for one. I then came home, and went whistling all over the house, much pleased with my whistle, but disturbing all the family. My brothers, and sisters, and cousins, understanding the bargain I had made, told me I had given four times as much for it as it was worth; put me in mind what good things I might have bought with the rest of the money; and laughed at me so much for my folly, that I cried with vexation; and the reflection gave me more chagrin than the whistle gave me pleasure.

This, however, was afterwards of use to me, the impression continuing on my mind; so that often, when I was tempted to buy some unnecessary thing, I said to myself, Don’t give too much for the whistle; and I saved my money.

As I grew up, came into the world, and observed the actions of men, I thought I met with many, very many, who gave too much for the whistle.

When I saw one too ambitious of court favor, sacrificing his time in attendance on levees, his repose, his liberty, his virtue, and perhaps his friends, to attain it, I have said to myself, This man gives too much for his whistle.  

When I saw another fond of popularity, constantly employing himself in political bustles, neglecting his own affairs, and ruining them by that neglect, He pays, indeed, said I, too much for his whistle.  

If I knew a miser, who gave up every kind of comfortable living, all the pleasure of doing good to others, all the esteem of his fellow-citizens, and the joys of benevolent friendship, for the sake of accumulating wealth, Poor man, said I, you pay too much for your whistle.  

When I met with a man of pleasure, sacrificing every laudable improvement of the mind, or of his fortune, to mere corporeal sensations, and ruining his health in their pursuit, Mistaken man, said I, you are providing pain for yourself, instead of pleasure; you give too much for your whistle.  

If I see one fond of appearance, or fine clothes, fine houses, fine furniture, fine equipages, all above his fortune, for which he contracts debts, and ends his career in a prison, Alas! say I, he has paid dear, very dear, for his whistle.

When I see a beautiful sweet-tempered girl married to an ill-natured brute of a husband, What a pity, say I, that she should pay so much for a whistle!  

In short, I conceive that great part of the miseries of mankind are brought upon them by the false estimates they have made of the value of things, and by their giving too much for their whistles.  

Yet I ought to have charity for these unhappy people, when I consider that, with all this wisdom of which I am boasting, there are certain things in the world so tempting, for example, the apples of King John, which happily are not to be bought; for if they were put to sale by auction, I might very easily be led to ruin myself in the purchase, and find that I had once more given too much for the whistle.