char to decimal

  • Thread starter Dirk Bruere at NeoPax
  • Start date
M

Michael Wojcik

Jeff said:
Yes, the nested if/else seems more clear in the above short example.

And that shows why context is often important in a question of coding
style. Your short example contained nothing that could justify the odd
use of the switch statement. (And your Java version made even less
sense, since it lost the C++ boolean-int conversions and symmetry.)
Now I wonder about the original author's motivation for
the use of switch statement. Code clarity, optimization, idiomatics?

It looks to me like the C++ code may have been based on a C
implementation of the algorithm, which would have been a bit simpler,
since the int casts would have been unnecessary:

switch (((d2 > curve_collinearity_epsilon) << 1) +
(d3 > curve_collinearity_epsilon)) {

I would have preferred binary-or there rather than addition, for
clarity, but either works.

And that, in turn, may have come from a description of the algorithm
that treated the two tests for inner-point collinearity as a pair of
bits (which is what this code is doing). Or it may have originally
been implemented on a processor where {test, test, shift, add,
computed-goto} was a faster sequence than {test, branch, test,
branch}. Bezier-curve rendering is a plausible target for optimization.

Or it may just be idiosyncrasy, particularly since this is a directly
recursive implementation, which seems likely to dwarf any savings from
fooling with the collinearity tests.

It's unlikely a Java implementation will benefit detectably from
anything other than the straightforward cascading-if-else design.
Certainly that would be the one to start with, and only investigate
alternatives if performance is a problem and profiling indicates this
is a useful target for optimization.
 
A

Andreas Leitgeb

Michael Wojcik said:
It's unlikely a Java implementation will benefit detectably from
anything other than the straightforward cascading-if-else design.
Certainly that would be the one to start with, and only investigate
alternatives if performance is a problem and profiling indicates this
is a useful target for optimization.

The more those poor-man's bitsets have to be passed around or stored
"en masse", rather than just being assembled and used (as in the switch
example), the more it will pay.
 
J

Jeff Higgins

And that shows why context is often important in a question of coding
style. Your short example contained nothing that could justify the odd
use of the switch statement. (And your Java version made even less
sense, since it lost the C++ boolean-int conversions and symmetry.)

Yes, a hasty post.
It looks to me like the C++ code may have been based on a C
implementation of the algorithm, which would have been a bit simpler,
since the int casts would have been unnecessary:

switch (((d2> curve_collinearity_epsilon)<< 1) +
(d3> curve_collinearity_epsilon)) {

I would have preferred binary-or there rather than addition, for
clarity, but either works.

Yes. I believe that if I had spent even a few more moments studying
I would have come up with markspace's one line solution.
Nigel Wade's one liner would have likely escaped me. Very nice.
Thanks to both.
And that, in turn, may have come from a description of the algorithm
that treated the two tests for inner-point collinearity as a pair of
bits (which is what this code is doing). Or it may have originally
been implemented on a processor where {test, test, shift, add,
computed-goto} was a faster sequence than {test, branch, test,
branch}. Bezier-curve rendering is a plausible target for optimization.

Or it may just be idiosyncrasy, particularly since this is a directly
recursive implementation, which seems likely to dwarf any savings from
fooling with the collinearity tests.

It's unlikely a Java implementation will benefit detectably from
anything other than the straightforward cascading-if-else design.
Certainly that would be the one to start with, and only investigate
alternatives if performance is a problem and profiling indicates this
is a useful target for optimization.

My translation of Maxim Shemanarev's code seems to run without a hitch
in an interactive Swing panel.

Roedy Green sparked my interest in this subject in a recent cljh post.
My original question was: Will the AGG algo produce a 'nicer' curve
than Java's FlatteningPathIterator? The answer so far is yes and no.

My interest has now expanded. Starting here, I've a ways to go.
<http://www3.villanova.edu/maple/misc/history_of_curvature/index.htm>

Thanks for the discussion, much appreciated.
 
L

Lawrence D'Oliveiro

switch (((d2 > curve_collinearity_epsilon) << 1) +
(d3 > curve_collinearity_epsilon)) {

I would have preferred binary-or there rather than addition, for
clarity, but either works.

Using the bitwise operator allows you to remove some parentheses.
 
M

Michael Wojcik

Lawrence said:
Using the bitwise operator allows you to remove some parentheses.

True. (In C, addition has higher precedence than bit-shift, but
bitwise-or has lower precedence than bit-shift.) Whether that makes
the overall expression easier to read is a subjective question, but
I'd probably go with removing the unnecessary parentheses,
particularly with the two subexpressions on separate lines (assuming
we keep the formatting).
 
M

Michael Wojcik

Jeff said:
Roedy Green sparked my interest in this subject in a recent cljh post.
My original question was: Will the AGG algo produce a 'nicer' curve
than Java's FlatteningPathIterator? The answer so far is yes and no.

My interest has now expanded. Starting here, I've a ways to go.
<http://www3.villanova.edu/maple/misc/history_of_curvature/index.htm>

Yes, and in terms of computer graphics, now that you've experimented
with Bezier curves you can move on to Bezier splines, then to more
general B-splines. Back when I was in graphics (over twenty years
ago), a popular curve primitive was NURBS - non-uniform rational
B-splines. NURBS let you specify different (hence "non-uniform")
weights for the "knots" (control points), which affects how much they
influence curvature.

Then generalize them to surfaces for some real fun.
 
L

Lawrence D'Oliveiro

Whether that makes the overall expression easier to read is a subjective
question, but I'd probably go with removing the unnecessary parentheses,
particularly with the two subexpressions on separate lines (assuming
we keep the formatting).

Try this (my preferred two-dimensional formatting style):

switch
(
(d2 > curve_collinearity_epsilon) << 1
|
(d3 > curve_collinearity_epsilon)
)
{
 
J

Jeff Higgins

switch
(
(d2> curve_collinearity_epsilon)<< 1
|
(d3> curve_collinearity_epsilon)
)

class Scratch {

int main() {
double d2;
double d3;
double e;
switch ((d2 > e) << 1 | (d3 > e))
{
case 0:
case 1:
case 2:
case 3:
default:
}
}
}

Scratch.java:7: operator << cannot be applied to boolean,int
switch ((d2 > e) << 1 | (d3 > e))
^
Scratch.java:7: incompatible types
found : boolean
required: int
switch ((d2 > e) << 1 | (d3 > e))
^
2 errors
 
J

Jeff Higgins

Roedy Green sparked my interest in this subject in a recent cljh post.
My original question was: Will the AGG algo produce a 'nicer' curve
than Java's FlatteningPathIterator? The answer so far is yes and no.

My interest has now expanded. Starting here, I've a ways to go.
<http://www3.villanova.edu/maple/misc/history_of_curvature/index.htm>
For my own future reference:
<http://home.comcast.net/~k9dci/site/?/page/Piecewise_Polynomial_Interpolation/>
Some help with curves for those of us with severe mathematical disabilities.
 
R

Roedy Green

For my own future reference:
<http://home.comcast.net/~k9dci/site/?/page/Piecewise_Polynomial_Interpolation/>
Some help with curves for those of us with severe mathematical disabilities.

A few thoughts. I studied various types of interpolation back when I
was a teenager. I had a friend trying to use a novel device called a
pen plotter. The goal was to write a demo program that would draw the
pink panther to use for open house at UBC. Ordinary polynomial
interpolation turned out to be very unstable. It seemed to be
constantly trying to misinterpret your intent while keeping to the
letter of the specification.

I tried to ask various advanced mathematicians what advice they had,
but since the idea of using equations to draw cartoons was unheard of
they were quite brusque with me. It was just too nutty and frivolous
an idea to consider.

The great Anthony Ralston came to visit (the author of our numerical
analysis textbook). Without having the proper vocabulary, I tried to
describe the spline curve, or a curve that mimicked a K&E
interpolation snake. He sharply chastised me saying it made no sense
at all to investigate curves that just looked a certain way without
having some mathematical meaning.

I asked Dr. Z. Melzak. He suggested Chebychev polynomials might be
better behaved. These also had the nice properly they were easier to
compute on the low res floating point of the era.

Too bad that I did not get a chance to ask Dr. John Warnock, who was
in Vancouver at the time, perhaps thinking about adding spline curves
and other interpolation to his future PostScript.

I wrote Walt Disney, explaining a scheme I had for interpolating (in
time) hand drawn cartoons (which would be called today "morphing"). I
figured it would lower costs and bring it a new golden era of
cartoons. They wrote back saying they were not interested.

Since that time there has an explosion in mathematical drawing, so I
would imagine there are now some "nice" algorithms, perhaps that
behave like the K&E dark green snake (do they still make those?)
--
Roedy Green Canadian Mind Products
http://mindprod.com
One of the great annoyances in programming derives from the irregularity
of English spelling especially when you have international teams.
I want to find a method or variable, but I don't know precisely
how its is spelled or worded. English is only approximately phonetic.
Letters are randomly doubled. The dictionary often lists variant spellings.
British, Canadian and American spellings differ.I would like to see an
experiment where variable names were spelled in a simplified English, where
there were no double letters.I also think you could add a number of rules
about composing variable names so that a variable name for something would
be highly predictable. You would also need automated enforcement of the
rules as well as possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top