How to intersect two polygons?

Arash Partow’s FastGEO library contains implementations of many interesting algorithms in computational geometry. Polygon intersection is one of them. It’s written in Pascal, but it’s only implementing math so it’s pretty readable. Note that you will certainly need to preprocess your edges a little, to get them into clockwise or counterclockwise order. ETA: But really, … Read more

How to fill with different colors between two lines? (originally: fill geom_polygon with different colors above and below y = 0 (or any other value)?)

Get the indices where the y value of two consecutive time steps have different sign. Use linear interpolation between these points to generate new x values where y is zero. First, a smaller example to make it easier to get a feeling for the linear interpolation and which points are added to the original data: … Read more

ploting filled polygons in python

I’m not exactly sure what matlab does, but you can draw a polygon using matplotlib.patches.Polygon. Adapted from an example in the docs: import numpy as np import matplotlib.pyplot as plt import matplotlib from matplotlib.patches import Polygon from matplotlib.collections import PatchCollection fig, ax = plt.subplots() patches = [] num_polygons = 5 num_sides = 5 for i … Read more

OpenCV threshold with mask

In general, you can simply compute the threshold using cv::threshold, and then copy the src image on dst using the inverted mask. // Apply cv::threshold on all image thresh = cv::threshold(src, dst, thresh, maxval, type); // Copy original image on inverted mask src.copyTo(dst, ~mask); With THRESH_OTSU, however, you also need to compute the threshold value … Read more