Programming/maths puzzler
Author
Discussion

judas

Original Poster:

6,211 posts

283 months

Tuesday 1st June 2004
quotequote all
Ok - another one for the PH programming brainiacs...

I'm trying to write a bit of code that checks whether two events overlap in any way and seeing as I've not done any more maths than adding up my shopping list since leaving school *mumble* years ago I'm a bit stuck.

Let's say we have the following variables:

x = Event 1 start time
y = Event 1 length in hours
a = Event 2 start time
b = Event 2 length in hours

Basically I need a true/false response to determine whether there's a clash and therefore allow the event to be booked or not.

All I need is a formula, but if anyone knows how to do this in ASP/VBScript that would be nice

Plotloss

67,280 posts

294 months

Tuesday 1st June 2004
quotequote all
z=x+y

If a<<z then error

Or am I misunderstanding?

>> Edited by Plotloss on Tuesday 1st June 12:35

Bonce

4,339 posts

303 months

Tuesday 1st June 2004
quotequote all
Plotloss: Looks sound, unless there's a chance that event 2 could start before event 1. In which case you have to repeat the same logic with opposite events.

annodomini2

6,964 posts

275 months

Tuesday 1st June 2004
quotequote all
he's basically right,

z= x+y;

if (a>z)
{
// book time
}
else
{
// raise clash error
}

Plotloss

67,280 posts

294 months

Tuesday 1st June 2004
quotequote all
or iterate through all available events and durations to build a calendar and then check?

_dobbo_

14,619 posts

272 months

Tuesday 1st June 2004
quotequote all
That formula doesn't allow for event 2 to start before event 1...

This page is all about date time functions in visual basic and comparisons of them...

www.officecomputertraining.com/vbtutorial/tutpages/page32.asp

judas

Original Poster:

6,211 posts

283 months

Tuesday 1st June 2004
quotequote all
Just to clarify: the start times and lengths of events 1 and 2 are wholly independent of each other and yes, event 2 could start before event 1.

greenv8s

30,999 posts

308 months

Tuesday 1st June 2004
quotequote all
This is a familiar graphics problem. Call the four times a1 a2 and b1 b2.

If you work out all the different combinations you will find there are six cases, four where the two intervals overlap and two where they don't. The ones where there is no overlap are (b2 < a1) and (b1 > a2).

judas

Original Poster:

6,211 posts

283 months

Tuesday 1st June 2004
quotequote all
greenv8s said:
This is a familiar graphics problem. Call the four times a1 a2 and b1 b2.

If you work out all the different combinations you will find there are six cases, four where the two intervals overlap and two where they don't. The ones where there is no overlap are (b2 < a1) and (b1 > a2).


Brilliant! Just the solution I needed. Many thanks to all everyone