Add a JavaScript function to obtain the vertices of a Polygon.

This commit is contained in:
Glen Whitney 2018-09-05 00:37:49 -04:00
parent e2f0f34394
commit bb1c2813be
2 changed files with 59 additions and 16 deletions

View File

@ -53,8 +53,13 @@ public class JSFunctions {
static String[] allnames={"cm", "Input", "Entrée", "EntréeNombreEntier", "EntréeNombreDécimal", "Prompt", "Signaler", "Print", "Afficher", "afficher", "Println", "Afficherligne", "Shownames", "MontrerNoms", "Hidenames", "CacherNoms", "Hide", "Cacher", "Show",
"Montrer", "Point", "PointOn", "PointSur", "ImplicitPlot", "TracéImplicite", "Intersection", "Intersection2", "Intersections", "MidPoint", "Milieu", "Symmetry", "SymétrieCentrale", "Line", "Droite", "Segment", "Circle", "Cercle", "Parallel",
"Parallèle", "Perpendicular", "Perpendiculaire", "FixedCircle", "CercleRayon", "Move", "Déplacer", "X", "Y", "GetExpressionValue", "PrendreValeurExpression", "Ray", "DemiDroite", "Angle", "Polygon", "Polygone", "Quadric", "Quadrique",
"CartesianFunction", "FonctionCartésienne", "ParametricFunction", "FonctionParamétrique", "SetColor", "MettreCouleur", "SetText", "MettreTexte", "SetThickness", "MettreEpaisseur", "SetFixed", "MettreFixe", "Vector", "Vecteur", "SetRGBColor",
"Parallèle", "Perpendicular", "Perpendiculaire", "FixedCircle", "CercleRayon",
"Move", "Déplacer", "X", "Y", "GetExpressionValue", "PrendreValeurExpression",
"Ray", "DemiDroite", "Angle", "Polygon", "Polygone", "Vertices", "Sommets",
"Quadric", "Quadrique", "CartesianFunction", "FonctionCartésienne",
"ParametricFunction", "FonctionParamétrique", "SetColor", "MettreCouleur",
"SetText", "MettreTexte", "SetThickness", "MettreEpaisseur", "SetFixed",
"MettreFixe", "Vector", "Vecteur", "SetRGBColor",
"MettreCouleurRVB", "SetShowName", "MettreMontrerNom", "SetShowValue", "MettreMontrerValeur", "SetFilled", "MettreRempli", "SetSolid", "MettreOpaque", "SetPartial", "MettrePartiel", "Expression", "Text", "Texte", "SetAlias", "MettreAlias", "SetMagneticObjects",
"MettreObjetsMagnétiques", "AddMagneticObject", "AjouterObjetMagnétique", "SetMagneticRay", "MettreRayonMagnétique", "SetPointType", "MettreTypePoint", "InteractiveInput", "EntréeInteractive", "FixedSegment", "SegmentFixe", "SetHide", "MettreCaché", "Pause",
"Delete", "Supprimer", "SetExpressionValue", "MettreValeurExpression", "Reflection", "SymétrieAxiale", "Translation", "PerpendicularBisector", "Médiatrice", "AngleBisector", "Bissectrice", "Circle3pts", "Cercle3pts",
@ -5877,7 +5882,7 @@ public class JSFunctions {
/**
* Creates a polygon define by a list of points. e.g. Polygon("","A,B,C")
* will create the triangle ABgetC().
* will create the triangle ABC.
* @param name Name of the polygon (suggestion)
* @param params list of objects
* @return Name of the created polygon
@ -5931,7 +5936,7 @@ public class JSFunctions {
return LastNObjectsName(1);
}
}
static public String Polygone3D(String name, String params) throws Exception{
synchronized (getC()) {
if (params.equals("undefined")) {
@ -6023,6 +6028,38 @@ public class JSFunctions {
return LastNObjectsName(1);
}
}
/**
* Obtains the list of vertices of a polygon, for example if "t" is the
* the triangle created by Polygon("t","A,B,C"), returns the comma-separated
* string "A,B,C".
* @param name Name of the polygon
* @return Comma-separated string of the names of the vertices
* */
static public String Vertices(String name) {
synchronized (getC()) {
name = parseVariables(name);
ConstructionObject o = getC().find(name);
if (o == null) { return ""; }
if (!(o instanceof AreaObject)) { return ""; }
return ((AreaObject) o).getVertexString(",");
}
}
/* Why isn't this
* static public String Sommets(String nom) { return Vertices(nom); }
* ???
*/
static public String Sommets(String name) {
synchronized (getC()) {
name = parseVariables(name);
ConstructionObject o = getC().find(name);
if (o == null) { return ""; }
if (!(o instanceof AreaObject)) { return ""; }
return ((AreaObject) o).getVertexString(",");
}
}
static public String Triangle(String name, String som1, String som2, String som3) {
synchronized (getC()) {

View File

@ -92,21 +92,27 @@ public class AreaObject extends ConstructionObject implements InsideObject,
return "Polygon";
}
public String getVertexString(String sep) {
String ret = "";
boolean first = true;
final Enumeration en = V.elements();
while (en.hasMoreElements()) {
final PointObject p = (PointObject) en.nextElement();
if (!first) {
ret = ret + sep;
} else {
first = false;
}
ret = ret + p.getName();
}
return ret;
}
@Override
public void updateText() {
String Text=Global.name("text.area");
final Enumeration en=V.elements();
boolean first=true;
while (en.hasMoreElements()) {
final PointObject p=(PointObject) en.nextElement();
if (!first) {
Text=Text+", ";
} else {
Text=Text+" ";
}
first=false;
Text=Text+p.getName();
}
Text = Text + " ";
Text = Text + getVertexString(", ");
setText(Text);
}