Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Main where
- import System.Random
- import Data.Graph
- import Data.List
- import Data.Maybe
- import qualified GXL as GXL
- import Text.XML.HaXml.XmlContent
- import Text.XML.HaXml.OneOfN
- type Point = (Int, Int)
- type Station = (Point, Int, [Int])
- gGraph :: Point -> Int -> Int -> Int
- -> IO (Graph, Vertex -> Station, Int -> Maybe Vertex)
- gGraph c r n rn = do
- points <- gPoints c r n
- return $ graphFromEdges $ gStations points rn
- gPoints :: (Int, Int) -> Int -> Int -> IO [Point]
- gPoints _ _ 0 = return []
- gPoints c r n = do
- point <- gPoint c r
- rest <- gPoints c r (n-1)
- return $ point : rest
- gPoint :: (Int, Int) -> Int -> IO Point
- gPoint c@(x, y) r = do
- x1 <- randomRIO (x - r, x + r)
- y1 <- randomRIO (y - r, y + r)
- if ((x1-x)^2 + (y1-y)^2 <= r^2)
- then return (x1, y1)
- else gPoint c r
- gStations :: [Point] -> Int -> [Station]
- gStations list range = foldl' (\a e -> a ++ [gStation e list range (length a)]) [] list
- where
- len = length list
- gStation :: Point -> [Point] -> Int -> Int -> Station
- gStation p list range k = (p, k, edges')
- where
- edges' :: [Int]
- edges' = foldr (\e a ->
- if (chDistance p e range)
- then (fromMaybe (-1) $ elemIndex e list) : a
- else a
- ) [] $ drop (k+1) list
- chDistance :: Point -> Point -> Int -> Bool
- chDistance (x1,y1) (x2,y2) r = d <= (4*(r^2))
- where
- d = ((x2-x1)^2) + ((y2-y1)^2)
- toGXL :: (Graph, Vertex -> Station, Int -> Maybe Vertex)
- -> Int
- -> GXL.Gxl
- toGXL (g, v, n) range = GXL.Gxl (GXL.Gxl_Attrs $ Default "") $ GXL.Graph
- (GXL.Graph_Attrs "stacje" Nothing
- (NonDefault GXL.Graph_edgeids_false)
- (NonDefault GXL.Graph_hypergraph_false)
- (NonDefault GXL.Graph_edgemode_defaultundirected))
- Nothing
- []
- ((toNodes (vertices g) v range) ++ (toEdges (vertices g) v)) : []
- toNodes :: [Vertex] -> (Vertex -> Station) -> Int
- -> [(OneOf3 GXL.Node GXL.Edge GXL.Rel)]
- toNodes vert v range = foldr (\e a ->
- (OneOf3 $ GXL.Node
- (GXL.Node_Attrs $ nodeid $ v e)
- Nothing
- [ GXL.Attr (GXL.Attr_Attrs Nothing "range" Nothing) []
- $ ThreeOf10 $ GXL.AInt $ show range
- , GXL.Attr (GXL.Attr_Attrs Nothing "center" Nothing) []
- $ FiveOf10 $ GXL.AString $ center $ v e
- ]
- []
- ):a) [] vert
- where
- nodeid (_, a, _) = show a
- center (a, _, _) = show a
- toEdges :: [Vertex] -> (Vertex -> Station)
- -> [(OneOf3 GXL.Node GXL.Edge GXL.Rel)]
- toEdges vert v = foldr (\ee a -> (foldr (\e aa ->
- (TwoOf3 $ GXL.Edge
- (GXL.Edge_Attrs
- Nothing
- (show $ key' $ v ee)
- (show e)
- Nothing
- Nothing
- Nothing )
- Nothing
- []
- []
- ):aa) [] $ edges' $ v ee)++a) [] vert
- where
- edges' (_, _, a) = a
- key' (_,k,_) = k
- main = do
- putStrLn "Podaj współrzędne środka (x,y): "
- center <- getLine
- putStrLn "Podaj promień koła: "
- radius <- getLine
- putStrLn "Podaj liczbę stacji: "
- num <- getLine
- putStrLn "Podaj zasięg stacji: "
- range <- getLine
- graph <- gGraph ((read center)::(Int,Int))
- ((read radius)::Int)
- ((read num)::Int)
- ((read range)::Int)
- putStrLn $ showXml False $ toGXL graph ((read range)::Int)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement