Python How To Check the Point is inside the Polygon


# determine if a point is inside a given polygon or not
# Polygon is a list of (x,y) pairs

def point_inside_polygon(x, y, poly):
n = len(poly)
inside = False

p1x, p1y = poly[0]
for i in range(n + 1):
p2x, p2y = poly[i % n]
if y > min(p1y, p2y):
if y <= max(p1y, p2y):
if x <= max(p1x, p2x):
if p1y != p2y:
xinters = (y – p1y) * (p2x – p1x) / (p2y – p1y) + p1x
if p1x == p2x or x <= xinters:
inside = not inside
p1x, p1y = p2x, p2y

return inside

Advertisements

XML Transformation

JDOM Way:

TransformerFactory tFactory = TransformerFactory.newInstance();
SAXBuilder builder = new SAXBuilder();

Document myDocument =  builder.build(new File(“path to xml File”);

// Make the input sources for the XML and XSLT documents
org.jdom.output.DOMOutputter outputter = new org.jdom.output.DOMOutputter();
JDOMSource source = new JDOMSource(myDocument);
JDOMResult result = new JDOMResult();

File xsltFile = new File(“C:/dev/kd/xml/data.xsl”);

StreamSource xsltSource =
new StreamSource(new FileInputStream(xsltFile));

FileOutputStream out = new FileOutputStream(new File(“path to output”);
// Get a XSLT transformer
Transformer transformer = tFactory.newTransformer(xsltSource);

// Do the transform
transformer.transform(source, result);
Document outDoc = result.getDocument();
XMLOutputter outp = new XMLOutputter();
outp.output(outDoc,out);

JDK javax.xml package:

TransformerFactory tFactory = TransformerFactory.newInstance();

File xsltFile = new File(“C:/dev/kd/xml/data.xsl”);

Templates template = tFactory.newTemplates(new StreamSource(
new FileInputStream(xsltFile)));
Transformer transformer = template.newTransformer();

// Do the transform
Source source = new StreamSource(new BufferedInputStream(new FileInputStream(new File(“Path to xml source”)));
Result result = new StreamResult(new FileOutputStream(new File(“path to output”));
Result printResult = new StreamResult(System.out);

transformer.transform(source, result);
source = new StreamSource(new FileInputStream(xmlFile));
transformer.setOutputProperty(OutputKeys.INDENT, “yes”);
transformer.transform(source, printResult);

Java Enum

Java enum can be really tricky like this:

package org.kd.examples;

public class EnumEx
{

private enum Column {
A(“a”,1)
{
public void execute(String value)
{
setExpression(“This is ” + A.title+ ” with ” + value);
};
},

B(“b”,2)
{
public void execute(String value)
{
setExpression(“This is ” + B.title+ ” with ” + value);
};
},

C(“c”,3)
{
public void execute(String value)
{
setExpression(“This is ” + C.title+ ” with ” + value);
};
};

public abstract void execute(String value);

private String title;
private int index;
private String expression;

public void setExpression(String value)
{
expression = value;
}
Column(String str, int i)
{
title = str;
index = i;
}

public String getExpression(){ return expression;}
}

public static Column getColumn(int i)
{
for(Column c : Column.values())
{
if(c.index == i)
return c;
}
return null;
}

public static void main(String argv[])
{
StringBuilder desc = new StringBuilder();

for(int i= 1 ; i < 4; i++)
{
Column c = getColumn(i);
c.execute(String.valueOf(i +4));
}

for(Column c: Column.values())
System.out.println(c.getExpression());
}

}