code got much more simplified

used new algorithm using dy and dx