Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Difficult(ish) SQL

Author  Topic 

bigshot
Starting Member

5 Posts

Posted - 2007-08-07 : 06:26:32
Scenario: You have a motherboard, on the motherboard are square/rectangle shaped transistors. Each transistors position is modelled in a simple table with their ID and XL, YL, XU, YU points, where the L's are the coordinates of the bottom left corner of the transistor, and the U's are the coordinates of the top right corner. Since there are only two coordinates I need to work out the others using those first of all.

The problem - if I am to insert another transistor (with given coordinates), I need to write a check to see whether this transistor can be placed on the motherboard without touching/intersecting/overlapping the AREA enclosed by all/any of the other existing transistors.

Like I said this is pretty difficult (especially for a beginner like me, hence I'm asking for help).
Assume the motherboard to be a 2dimensional grid, and say theres two existing transistors. 1 - has coordinates XL=0, YL=0, XU=2, YU=1 (it is in the corner of the grid and spans two squares/units wide and one unit high). 2 - has coordinates XL=5, YL=0, XU=8, YU=2.
And the one I want to insert has coordinates XL=1, YL=0, XU=3, YU=2 (so it will overlap with one of the existing transistors).

So any help on this tricky example would be greatly appreciated.

Ifor
Aged Yak Warrior

700 Posts

Posted - 2007-08-07 : 08:05:50
Maybe you could use a trigger, as below, or else you could just apply the condition on each insert.

USE tempdb
GO
CREATE TABLE dbo.MotherBoardTransistors
(
MotherBoardID int NOT NULL
,TransitorID int NOT NULL
,XL int NOT NULL
,YL int NOT NULL
,XU int NOT NULL
,YU int NOT NULL
)
GO
CREATE TRIGGER MotherBoardTransistorsIU
ON dbo.MotherBoardTransistors
AFTER INSERT, UPDATE
AS

IF EXISTS (
SELECT *
FROM dbo.MotherBoardTransistors T
JOIN inserted I
ON T.MotherBoardID = I.MotherBoardID
AND T.TransitorID <> I.TransitorID
AND (
I.XL BETWEEN T.XL AND T.XU
OR I.XU BETWEEN T.XL AND T.XU
)
AND (
I.YL BETWEEN T.YL AND T.YU
OR I.YU BETWEEN T.YL AND T.YU
)
)
BEGIN
RAISERROR ('Transistor Overlap.', 16, 1)
ROLLBACK
END
GO

INSERT INTO dbo.MotherBoardTransistors
SELECT 1, 1, 0, 0, 2, 1
GO
INSERT INTO dbo.MotherBoardTransistors
SELECT 1, 2, 5, 0, 8, 2
GO
INSERT INTO dbo.MotherBoardTransistors
SELECT 1, 3, 1, 0, 3, 2
GO

--DROP TABLE dbo.MotherBoardTransistors
Go to Top of Page

bigshot
Starting Member

5 Posts

Posted - 2007-08-07 : 15:48:33
thanks for the suggestion, I will try it out today.
Go to Top of Page

bigshot
Starting Member

5 Posts

Posted - 2007-08-08 : 03:42:00
Not bad, I haven't tried out all the possibilities (just the code as you gave it), but that seems to work, thanks a lot.
Go to Top of Page
   

- Advertisement -