Pregunta

The question is, is there a straightforward mechanistic way to know which stars (gold, 1/2 gold, grey) to draw without using conditional logic, for example using a pointer to the correct star based on the result of a simple math function to generate star ratings in a five star display.

The point of this question is not Ratings Systems, but graphic display

I have a rating value of 0-100, floating point.

For example, in pseudocode:

bitmap1="http://myserver.com/goldstar.png"
bitmap2="http://myserver.com/halfstar.png"
bitmap3="http://myserver.com/greystar.png"
rating=89.003
possible=100
quantized=int(rating/possible)
imagearray=[bitmap1,bitmap2,bitmap3]

for i=0 to 4
    selector=<compute which star to draw based on available data>
    drawstars(25*i,100,imagearray[selector])
end for

Hopefully that will give you an idea of what i'm trying to do.

¿Fue útil?

Solución

Think of the display as a scale, but in integers, so work in half stars. For example, if we have 5 stars, and can display half stars, our real scale is from 0 to 10. So, what we need to do is divide the original scale (0 to 100) down to give us 0 to 10. Then we do integer division by 2. The quotient of that will give us the number of complete stars to draw, and the remainder the number (0 or 1) of half stars to draw.

You may want to do some rounding first though -- otherwise, a rating of 5 stars will be essentially impossible to get (e.g., even an input score of 99 will still only give 4.5 stars).

Otros consejos

Hm, if I were to take a stab at it...

In the for loop, go from i=1 to 5.

if (rating > i*20){ draw a gold star }
else{
    if (rating > (i-1)*20 +10) {draw a half star }
    else {draw a grey star}
}

This of course would give you the floor rating

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top