Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
bmichotte committed Feb 28, 2019
2 parents 206a293 + b1d8b6a commit 48e258c
Showing 18 changed files with 549 additions and 621 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_brace_style = 1TBS
indent_style = space
indent_size = 4

[**.php]
indent_size = 4
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nbproject

vendor
/.phpunit.result.cache
/composer.lock
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tabs": 4,
"semi": false,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": false,
"jsxBracketSameLine": true,
"arrowParens": "avoid"
}
18 changes: 18 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
filter:
excluded_paths: [tests/*]

checks:
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sudo: required

language: php

php:
- 7.2

before_install:
- sudo apt-get update
- travis_retry composer self-update

install:
- travis_retry composer update --prefer-source $COMPOSER_FLAGS

script:
- vendor/bin/phpunit

branches:
only:
- master
- develop
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
php5 Dijkstra
========================
# Dijkstra algorithm implementation

This is an implementation of the Dijkstra algorithm.
[![Build Status](https://img.shields.io/travis/bmichotte/dijkstra/master.svg?style=flat-square)](https://travis-ci.org/bmichotte/dijkstra)
[![Quality Score](https://img.shields.io/scrutinizer/g/bmichotte/dijkstra.svg?style=flat-square)](https://scrutinizer-ci.com/g/bmichotte/dijkstra)

see index.php for an example
More on the algorithm : https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

You can find an example on the `example` directory. The result of this example should be something like
![shortest paths](https://github.com/bmichotte/dijkstra/blob/master/example/image.png)
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "bmichotte/dijkstra",
"description": "php 7+ implementation of the Dijkstra algorithm",
"version": "1.0.0",
"keywords": [
"dijkstra"
],
"homepage": "https://github.com/bmichotte/dijkstra",
"license": "MIT",
"authors": [
{
"name": "Benjamin Michotte",
"email": "[email protected]"
}
],
"require": {
"php": "^7.2"
},
"require-dev": {
"ext-gd": "*",
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
"Bmichotte\\Dijkstra\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Bmichotte\\Dijkstra\\Test\\": "tests"
}
},
"prefer-stable": true,
"scripts": {
"test": "vendor/bin/phpunit"
},
"config": {
"sort-packages": true
}
}
112 changes: 112 additions & 0 deletions example/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
use Bmichotte\Dijkstra\Point;
use Bmichotte\Dijkstra\Dijkstra;

function findLink(int $minDistance, array &$positions): void
{
foreach ($positions as $point) {
findLinkBetween($minDistance, $point, $positions);
}
}

function findLinkBetween(int $minDistance, Point &$point1, array &$positions): void
{
foreach ($positions as $point2) {
if ($point1->equals($point2)) {
continue;
}

$distance = Dijkstra::distance($point1, $point2);
if ($distance < $minDistance) {
$point1->addPoint($point2);
}
}

if (0 === count($point1->points)) {
findLinkBetween($minDistance * 2, $point1, $positions);
}
}

function findFromTo(array $positions): array
{
$from = null;
$to = null;
foreach ($positions as $point) {
$from = $from ?: $point;
$to = $to ?: $point;

if ($point->x < $from->x && $point->y < $from->y) {
$from = $point;
}

if ($point->x > $to->x && $point->y > $to->y) {
$to = $point;
}
}

return [$from, $to];
}

function drawPaths(int $max, array $positions, Point $from, Point $to, array $shortestPath, string $filename): void
{
// open background
$image = imagecreatetruecolor($max, $max);
$color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $color);

// first run, draw lines
$color = imagecolorallocate($image, 32, 230, 200);
foreach ($positions as $point) {
foreach ($point->points as $link) {
drawLine($image, $point, $link, $color);
}
}

// then, draw the points
$color = imagecolorallocate($image, 32, 230, 36);
foreach ($positions as $point) {
imagefilledellipse($image, $point->x, $point->y, 10, 10, $color);
}

// draw the shortest path
$color = imagecolorallocate($image, 255, 0, 255);
for ($i = 0; $i < count($shortestPath); $i++) {
$p = $shortestPath[$i];
if (isset($shortestPath[$i + 1])) {
$d = $shortestPath[$i + 1];
drawLine($image, $p, $d, $color, 3);
}
}

// and finally, draw the from and to points
$color = imagecolorallocate($image, 255, 0, 255);
imagefilledellipse($image, $from->x, $from->y, 10, 10, $color);
imagefilledellipse($image, $to->x, $to->y, 10, 10, $color);

imagepng($image, $filename);
}

function drawLine($image, Point $point1, Point $point2, $color, int $thick = 1): void
{
if (null === $point1 || null === $point2) {
return;
}
if (null === $point1->x || null === $point1->y) {
return;
}
if (null === $point2->x || null === $point2->y) {
return;
}

if ($point1->x === $point2->x) {
$from = $point1->y < $point2->y ? $point1 : $point2;
$to = $point1->y > $point2->y ? $point1 : $point2;
} else {
$from = $point1->x < $point2->x ? $point1 : $point2;
$to = $point1->x > $point2->x ? $point1 : $point2;
}

imagesetthickness($image, $thick);

imageline($image, $from->x, $from->y, $to->x, $to->y, $color);
}
Binary file added example/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Bmichotte\Dijkstra\Point;
use Bmichotte\Dijkstra\Dijkstra;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/helpers.php';

// number of nodes
$nodes = 150;

// maximum size of the map
$max = 800;

// distance between two nodes
$minDistance = 120;

// we add "$nodes" dynamic nodes
$positions = [];
foreach (range(0, $nodes) as $value) {
$positions[] = new Point(rand(0, $max), rand(0, $max));
}

// add random links
findLink($minDistance, $positions);

// find the most distant points
list($from, $to) = findFromTo($positions);

$dijkstra = new Dijkstra($positions, $from, $to);
$shortestPath = $dijkstra->findShortestPath();

// draw the result
drawPaths($max, $positions, $from, $to, $shortestPath, 'image.png');
Loading

0 comments on commit 48e258c

Please sign in to comment.