Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

home / study / engineering / computer science / computer science questions and a

ID: 3748228 • Letter: H

Question

home / study / engineering / computer science / computer science questions and answers / how to implement the graham's scan algorithm using jsxgraph using a code: convexhull.html i ...

Question: How to implement the GRAHAM'S SCAN algorithm using JSXGraph using a Code: ConvexHull.html I wrote...

Edit question

How to implement the GRAHAM'S SCAN algorithm using JSXGraph using a Code: ConvexHull.html

I wrote the code using Java Script - this part of the code has to be in JS Lang.

I have this code to implement a Graham's Scan algorithm using JSXGraph and this part of the code to draw a polygon on a board using a set of points.

This is the code:

</body>

I need help with implementing this part of the code:

/* The Graham's scan function */

function findConvexHull(board) {

var N = 0, P = {};

for(var el in board.objects)

if(board.objects[el].elType == 'point') {

P[N] = board.objects[el];

N++;

}

// sort the point set P; to obtain the x-coordinate of 'p', simply use 'p.X()' and similarly you can use 'p.Y()' for the y-coordinate

function p.X()

function p.Y()

// run Graham's scan to obtain the convex hull points in some order (clockwise/anticlokwise)

What to implement here?(Method)

/* draw line segments between the convex hull points to obtain the final convex polygon; to draw line segment you can simply use --> board.create('segment',[p1,p2],{fillColor: 'green',strokeColor:'green'}); where p1,p2 are the two end-points */

(How to implement the function)

board.create('segment',[p1,p2],

{fillColor: 'green',strokeColor:'green'});

}

Thanks for the help

<head> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraph.css" /> <script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraphcore.js"></script> </head> <body> <div id="jxgbox" class="jxgbox"> </div> <button id="button1" type="button" >Find convex hull</button> <div id="textDiv"></div> <script type="text/javascript"> /***************** Do not alter, unless required! ***************/ var boardParams = { boundingbox: [-0.2,50,50,-0.2], grid :true, axis: false, showCopyright:false, showNavigation:false, }; var getMouseCoords = function(e, i) { var cPos = board.getCoordsTopLeftCorner(e, i), absPos = JXG.getPosition(e, i), dx = absPos[0] - cPos[0], dy = absPos[1] - cPos[1]; return new JXG.Coords(JXG.COORDS_BY_SCREEN, [dx, dy], board); }; /*********************************************************************/ /* The Graham's scan function */ function findConvexHull(board) { var N = 0, P = {}; for(var el in board.objects) if(board.objects[el].elType == 'point') { P[N] = board.objects[el]; N++; } // sort the point set P; to obtain the x-coordinate of 'p', simply use 'p.X()' and similarly you can use 'p.Y()' for the y-coordinate // run Graham's scan to obtain the convex hull points in some order (clockwise/anticlokwise) // draw line segments between the convex hull points to obtain the final convex polygon; to draw line segment you can simply // use --> board.create('segment',[p1,p2],{fillColor:'green',strokeColor:'green'}); where p1,p2 are the two end-points } /*********************************************************************/ /* Mouse drawing function */ var down = function(e) { var canCreatePoint = true, i, coords; if (e[JXG.touchProperty]) i = 0; coords = getMouseCoords(e, i); var point = board.create('point',[coords.usrCoords[1],coords.usrCoords[2]], {highlightFillColor:'Gray', highlightStrokeColor:'Gray', size:2, fillColor:'RoyalBlue', strokeColor:'RoyalBlue'}); }; /*********************************************************************/ /* Create a board */ var board = JXG.JSXGraph.initBoard('jxgbox',boardParams); board.on('down', down); board.off('off', down); </script>

</body>

I need help with implementing this part of the code:

/* The Graham's scan function */

function findConvexHull(board) {

var N = 0, P = {};

for(var el in board.objects)

if(board.objects[el].elType == 'point') {

P[N] = board.objects[el];

N++;

}

// sort the point set P; to obtain the x-coordinate of 'p', simply use 'p.X()' and similarly you can use 'p.Y()' for the y-coordinate

function p.X()

function p.Y()

// run Graham's scan to obtain the convex hull points in some order (clockwise/anticlokwise)

What to implement here?(Method)

/* draw line segments between the convex hull points to obtain the final convex polygon; to draw line segment you can simply use --> board.create('segment',[p1,p2],{fillColor: 'green',strokeColor:'green'}); where p1,p2 are the two end-points */

(How to implement the function)

board.create('segment',[p1,p2],

{fillColor: 'green',strokeColor:'green'});

}

Thanks for the help

Explanation / Answer

/* The Graham's scan function */

function findConvexHull(board) {

var N = 0, P = {};

for(var el in board.objects)

if(board.objects[el].elType == 'point') {

P[N] = board.objects[el];

N++;

}

// sort the point set P; to obtain the x-coordinate of 'p', simply use 'p.X()' and similarly you can use 'p.Y()' for the y-coordinate

function p.X()

function p.Y()

// run Graham's scan to obtain the convex hull points in some order (clockwise/anticlokwise)

What to implement here?

/* draw line segments between the convex hull points to obtain the final convex polygon; to draw line segment you can simply use --> board.create('segment',[p1,p2],{fillColor: 'green',strokeColor:'green'}); where p1,p2 are the two end-points */

board.create('segment',[p1,p2],{fillColor: 'green',strokeColor:'green'});(How to implement the function)

}