P
P
Pavel K2015-12-25 14:06:56
Canvas
Pavel K, 2015-12-25 14:06:56

How to solve the problem when drawing on canvas with pixel coordinates, when fractional values ​​are obtained?

Greetings!
Simplified description:
I'm doing something like a calendar grid,
I'm drawing a rectangle 400 pixels wide - the month of February
has 29 days and you need them to be equal in width, i.e.
400/29 = 13.79 draw
29 vertical lines
values ​​can only be integers,
after the last column there is a lot of empty space, although it should not be =(
vyJhphfl.jpg
In the figure, all lines are 1 pixel wide.
Can you please tell me how to solve this problem more elegantly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivanq, 2015-12-25
@PavelK

...
var w = 400 / 29; // или 28
var x1, x2;
for(var i=0;i<29;i++) {
  x1 = Math.floor(i * w);
  x2 = Math.floor(x1 + w);
  // И тут рисуете прямоугольник от (x1, y1) до (x2, y2), где y1 - где заканчивается "февраль", а y2 - конец экрана.
}

I
Ivan, 2015-12-25
@LiguidCool

It seems that you have not one column there, but 2.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question