Pregunta

I am trying to write a lecture collision algorithm for a schedule application. Each lecture has a start and end date.

currentListElement is the existing lecture in my current schedule, and I want to add chosenLecture, and check if there is a collision between my current lectures. Therefore, this boolean expression should return true if collision occurs.

Thank you for your help

(currentListElement['startDate'] < chosenLecture['startDate'] 
  || currentListElement['startDate'] >= chosenLecture['endDate']) 
 && (currentListElement['endDate'] <= chosenLecture['startDate'] 
  || currentListElement['endDate'] > chosenLecture['endDate'])
¿Fue útil?

Solución

Actually there is a little mistake, try this:

(currentListElement['endDate'] < chosenLecture['startDate'] 
 || currentListElement['startDate'] > chosenLecture['endDate'])

There is no collision in two cases:

  1. The lecture is set entirely before the current one. To check that, just make sure it ends before the current starts.
  2. The lecture is set entirely after the current one. To check that, just make sure it starts after the current ends.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top