Drawing a rectangle by combining 4 plotted points from a blank graph

109 views
Skip to first unread message

Irshad Mujawar

unread,
Oct 5, 2016, 8:14:33 AM10/5/16
to JSXGraph
Hello everyone,

I can create a polygon with four already deifned points can put 4 points whihc makes a rectangle. 

However how to start from the blank graph. put 4 points on (x,y) coordinates , which will automatically combine to a square.

And later we should be able to access the coordinates of these points so that you can calculate area/perimeter.


Pictorially it should somewhat look like this:

Figure1 : Plot four points on the blank graph.





Figure 2: combine them to to for a rectangle/square like this:






Thanks and Regards,

Irshad




Eric Peters

unread,
Oct 5, 2016, 9:45:43 AM10/5/16
to JSXGraph
Hi Irshad,

The simplest way is to group these four points.

var p1 = board.create('point', [1, 1], pointAttributes);
var p2 = board.create('point', [4, 1], pointAttributes);
var p3 = board.create('point', [4, 4], pointAttributes);
var p4 = board.create('point', [1, 4], pointAttributes);

var group = board.create('group', [p1, p2, p3, p4]);

And you canwrite your own rectangle element where this is done "automatically". See the source code for file polygon.js. There is a polygon defined and a regular polygon, which is a special polygon. Your rectangle will also be a special polygon :-)

Best regards
Eric

Irshad Mujawar

unread,
Oct 5, 2016, 2:27:16 PM10/5/16
to JSXGraph
Thank you for the response Eric.
I know this part. The scenario I am describing,in that points will be placed on the blank graph.
I don't know those points coordinates in advance. I am finding it challenging to retreive those coordinates from the graph and use it in the code to further calculation like area.

Eric Peters

unread,
Oct 6, 2016, 2:48:14 AM10/6/16
to JSXGraph
Hi Irshad,

if you have only these four points in your board, you can use board.objectsLists to have access to all objects in the board. But pay attention, there are also some internal points and the lines for the coordinate system included. If you have a point you can access the coordinates by the getter functions X() and Y().

Is this a solution for your problem?

Best regards
Eric

Irshad Mujawar

unread,
Oct 6, 2016, 3:28:10 AM10/6/16
to JSXGraph
Hello Eric,


Thank you for the response. I am confused about once I run the code for creating point in the fiddle, I place my points on the coordinates. How can I access the points in the code which are going get plotted once I run the code.

I dont know how to proceed after this?







Regards,
Irshad

Alfred Wassermann

unread,
Oct 6, 2016, 5:31:16 AM10/6/16
to JSXGraph
Why don't you initialize an empty array at the beginning

var points = [];

and push the newly created points to that array?

        if (canCreate) {
            points
.push(board.create('point', [coords.usrCoords[1], coords.usrCoords[2]], {snapToGrid:true}));
       
}


Then you can access the points via points[0], points[1], ...

Best wishes,
Alfred



Message has been deleted

Irshad Mujawar

unread,
Oct 6, 2016, 6:16:25 AM10/6/16
to JSXGraph

That was really clever suggestion Alfred. It worked. Thank you so much. :)

Reply all
Reply to author
Forward
0 new messages