anyone good at math?
posted 5 years ago
i finally thought of a new project. it is an old style analog clock. it has to draw the hands every second. to do this it uses two points, the origin and a point on the circumference. i use 3 array[60] to hold the end points. my problem is with initializing the arrays. lets just consider the second hand for now, the others will be similar. this is what i came up with but it is so convoluted i think there has to be a better way.
O = the origin point
L = the length of the hand(radius)
I = the index into the array and the value returned by Calendar
i have four special cases
I = 0
{
x = O.x
y = O.y  L
}
I = 15
{
x = O.x + L
y = O.y
}
I = 30 {
x = O.x
y = O.y + L
}
I = 45
{
x = O.x  L
y = O.y
}
then i need four seperate for statements
for(1 to 14)
x = O.x + (L/15) * I
y = O.y  (L  (I/15) * L
for(16 to 29)
x = O.x + (L  ((I 15 )/15) * L)
y = O.y + (L/15) * (I  15)
that should be enough to give you the idea. please tell me there is a better way.
O = the origin point
L = the length of the hand(radius)
I = the index into the array and the value returned by Calendar
i have four special cases
I = 0
{
x = O.x
y = O.y  L
}
I = 15
{
x = O.x + L
y = O.y
}
I = 30 {
x = O.x
y = O.y + L
}
I = 45
{
x = O.x  L
y = O.y
}
then i need four seperate for statements
for(1 to 14)
x = O.x + (L/15) * I
y = O.y  (L  (I/15) * L
for(16 to 29)
x = O.x + (L  ((I 15 )/15) * L)
y = O.y + (L/15) * (I  15)
that should be enough to give you the idea. please tell me there is a better way.
SCJP
Visit my download page
Campbell Ritchie
Marshal
Posts: 52663
121
posted 5 years ago
Cast your mind back six years, to when I was a little greenhorn and I posted this.
After I had explained it facetoface to Joseph, I helped him implement a clock, and mischievously suggested trying sin instead of +sin. So he had this clock resolutely ticking away backwards and Elizabeth, the lecturer, came to see how we were getting on. I had to confess that I might have misled Jo slightly, and she laughed out loud when she saw the clock working.
After I had explained it facetoface to Joseph, I helped him implement a clock, and mischievously suggested trying sin instead of +sin. So he had this clock resolutely ticking away backwards and Elizabeth, the lecturer, came to see how we were getting on. I had to confess that I might have misled Jo slightly, and she laughed out loud when she saw the clock working.
posted 5 years ago
i almost have it working the way i did it, but i do thank you for the link. now i have something to do when i finish(rewrite it the other way)
SCJP
Visit my download page
posted 5 years ago
thanks again Campbell. it turns out the way i was doing it didn't work correctly anyway. the hand moved correctly, but it's length didn't stay the same. it would have worked fine if the clock was square(diamond shaped).
SCJP
Visit my download page
posted 5 years ago
ok, i really need help with this. in the 8 years i spent in college, the only course i ever failed was trigonometry. i took it the very next term and still only got a C. by comparison calculus and probability&statistics were easy. i saved Campbells first post in that old thread and worked on it offline. all i ended up with was this://g.drawLine(origin.x,origin.y,origin.x + Math.cos(angle) * secondHandLength,origin.y  Math.sin(angle) * secondHandLength);
it's good code once i have a variable called angle.
i will post the entire code since it isn't that long.
i have two observations. first i might not need the arrays. and second the hour and minute hands might move linearly instead of "ticking" like they would have in my original design. i will copy his second post in that old thread and see if i can figure it out, but any help will be appreciated.
it's good code once i have a variable called angle.
i will post the entire code since it isn't that long.
i have two observations. first i might not need the arrays. and second the hour and minute hands might move linearly instead of "ticking" like they would have in my original design. i will copy his second post in that old thread and see if i can figure it out, but any help will be appreciated.
SCJP
Visit my download page
Campbell Ritchie
Marshal
Posts: 52663
121
posted 5 years ago
Create a copy of the Graphics object, and cast it to Graphics 2d. I am not absolutely certain whether this step is necessary, but affine transforms can interfere with each other. Shear is the worst offender, and you are only using scale and translate, so you might not have any problems.
You will have to look in the API, because I am doing this from memory, and you will have to assume some values for the constants. They should be obvious.
You have a clock face, and you move the graphics object with its translate method, so the origin is in the middle of the componentNow you need to work out from the time what the angle is. Let us assume you have a minutes int local variable, such that 0 = 12o’clock and 719 = 11.59. Now, the origin of sin and cos work on the assumption that 0° = 3o’clock and they run anticlockwise.This is from memory, so I cannot guarantee it is correct; please tell me if you find an error. Note you have to double up the angles because a circle is not π radians but 2π radians round.
You will have to look in the API, because I am doing this from memory, and you will have to assume some values for the constants. They should be obvious.
You have a clock face, and you move the graphics object with its translate method, so the origin is in the middle of the componentNow you need to work out from the time what the angle is. Let us assume you have a minutes int local variable, such that 0 = 12o’clock and 719 = 11.59. Now, the origin of sin and cos work on the assumption that 0° = 3o’clock and they run anticlockwise.This is from memory, so I cannot guarantee it is correct; please tell me if you find an error. Note you have to double up the angles because a circle is not π radians but 2π radians round.
posted 5 years ago
thanks Campbell, i was just frustrated i couldn't figure it out. but after a good night sleep, i see i am very close. i don't need any arrays. i just have to figure out the 3 angles in actionPerformed(), then use them in paintComponent(). so i guess it is easy.
thanks also for the advice about shear i noticed that and thought my other program that drew lines didn't have that problem.
i posted the rest of this message before actually reading your reply. are you trying to confuse me?
your original link explains it pretty well.
i'll figure it out thanks to you though. and i will post the final source code and a link to the image file so you can all have a clock on me.
thanks also for the advice about shear i noticed that and thought my other program that drew lines didn't have that problem.
i posted the rest of this message before actually reading your reply. are you trying to confuse me?
your original link explains it pretty well.
i'll figure it out thanks to you though. and i will post the final source code and a link to the image file so you can all have a clock on me.
SCJP
Visit my download page
Campbell Ritchie
Marshal
Posts: 52663
121
posted 5 years ago
i finished it this morning.
i didn't use a layout either
it does have a little shear, but not too bad though
thanks for the help. i'm glad i didn't have to relearn trig so i could figure it out myself.
i didn't use a layout either
it does have a little shear, but not too bad though
thanks for the help. i'm glad i didn't have to relearn trig so i could figure it out myself.
SCJP
Visit my download page
Campbell Ritchie
Marshal
Posts: 52663
121
posted 5 years ago
Campbell, i tried what you first mentioned, using sin for x.
it's funny, seeing a digital clock counting down looks normal, but seeing an analog clock running backwards looks wrong. a lifetime of conditioning i suppose.
if i can find an image of a clock face with the numbers going counterclockwise i will use it. hehe
i am sure you are right about not needing the mod operators. especially since Calendar takes care of that.
it's funny, seeing a digital clock counting down looks normal, but seeing an analog clock running backwards looks wrong. a lifetime of conditioning i suppose.
if i can find an image of a clock face with the numbers going counterclockwise i will use it. hehe
i am sure you are right about not needing the mod operators. especially since Calendar takes care of that.
SCJP
Visit my download page
Campbell Ritchie
Marshal
Posts: 52663
121
posted 5 years ago
There is an analogue clock running anticlockwise about 10 feet beyond my left shoulder as I write. When I moved in here, I thought we are all computer scientists, we ought to be able to think laterally, so we can cope with an anticlockwise clock. There is one in the club where I pay £2.55 for a pint (0.57l) of beer. I see them occasionally in bars, where they ought to be placed opposite a mirror. So few people realise you can read an anticlockwise clock easily by looking in the mirror! I am tempted to buy my wife such a clock as a present, but I think that might cause ructions
The reason you don’t need the % operator is that sin, cos, etc., take any value for their argument; sin(x) is equal to sin(x % (2π)) for all values of x, with a few reasonable assumptions about sign when x < 0.
The reason you don’t need the % operator is that sin, cos, etc., take any value for their argument; sin(x) is equal to sin(x % (2π)) for all values of x, with a few reasonable assumptions about sign when x < 0.
posted 5 years ago
You had to take a whole course in trigonometry? I didn't think there was enough content for a whole course (I was a graduate teaching assistant for courses where we spent a week or two covering trigonometry).
The only thing you needed to take away from your trig course was the acronym SOHCAHTOA. (Draw a right triangle and pick one of the two nonrightangle corners; then Sine = Opposite/Hypotenuse, Cosine = Adjacent/Hypotenuse, and Tangent = Opposite/Adjacent.) This is pretty much all you'll ever need to know for programming.
Randall Twede wrote:thanks for the help. i'm glad i didn't have to relearn trig so i could figure it out myself.
You had to take a whole course in trigonometry? I didn't think there was enough content for a whole course (I was a graduate teaching assistant for courses where we spent a week or two covering trigonometry).
The only thing you needed to take away from your trig course was the acronym SOHCAHTOA. (Draw a right triangle and pick one of the two nonrightangle corners; then Sine = Opposite/Hypotenuse, Cosine = Adjacent/Hypotenuse, and Tangent = Opposite/Adjacent.) This is pretty much all you'll ever need to know for programming.
posted 5 years ago
it was not only a 3 month course at community college when i took it in the 70's, but it is the only course i failed and had to take again. this is the first time i have had to use it in over 30 years.
SCJP
Visit my download page
posted 5 years ago
Graphics2D#setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
Every Graphics object in a modern JDK (>1.2) is a Graphics2D subclass.
Randall Twede wrote:it does have a little shear, but not too bad though
Graphics2D#setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
Every Graphics object in a modern JDK (>1.2) is a Graphics2D subclass.
luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Marshal
Posts: 52663
121
posted 5 years ago
ah yes. aliasing. well it isn't very bad, but since i have nothing to do, i might pursue the hint D.B. gave me.
SCJP
Visit my download page
Campbell Ritchie
Marshal
Posts: 52663
121
posted 5 years ago
ah well, all the backward clocks i could find on google had hands so i would have to edit one in Paint to erase the hands. too much bother for now.
SCJP
Visit my download page
posted 5 years ago
here is something interesting. i cast the Graphics g to Grapics2D and called it g2.
Graphics2D g2 = (Graphics2D)g;
i set the antialiasing to on and ran it and it was much smoother. then i went back to the code to change the stroke to make the hour and minute hand wider and i noticed i was still using g to draw the lines. but the antialiasing worked anyway.
one question though. the current code is this
should i move the third line to before the other two and use g2 in those lines? does it matter?
Graphics2D g2 = (Graphics2D)g;
i set the antialiasing to on and ran it and it was much smoother. then i went back to the code to change the stroke to make the hour and minute hand wider and i noticed i was still using g to draw the lines. but the antialiasing worked anyway.
one question though. the current code is this
should i move the third line to before the other two and use g2 in those lines? does it matter?
SCJP
Visit my download page
Why is the word "abbreviation" so long? And this ad is so short?
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/ThreadBoostfeature
