The sawtooth wave does it automatically as an analog function. It is a geometric method to solve two simultaneous equations. Namely, where do the two graph lines intersect?
When we try to code it we must use a mathematical approach. I believe it's a calculus problem.
Start with the first time increment. Ask, what length does a pulse need to be, in order to create an area equal to the area found under the sine curve, during that time increment?
I think this works out about the same as taking the sine at that point on the curve, then set that pulse length in proportion to whatever you set as your maximum length.
Example, suppose pulses at the peak are same length as the time step.
...Then at the 20 degree mark, make that pulse 34% as long, because the sine of 20 degrees is 0.342.
...At the 40 degree mark, make that pulse 64% as long, because the sine of 40 deg is .643.
...At the 60 degree mark, make that pulse 87% as long, because the sine of 60 deg is .866.
Etc.