How to calculate line from a set of polygon points? - algorithm

Let's say now I have an array that used to describe the shape of a polygon:
[
[0.0, 1.0],
[0.5, 1.0],
[0.5, 0.3],
[1.0, 0.3],
[1.0, 0.0],
[0.0, 0.0]
]
As shown on diagram above, the blue line(s) or vector points list is the result I was looking for.
Assuming the polygon only composed of one or more rectangular shapes, is there any way to extract/simplify the polygon to one or more lines?

Related

Clustering N points based on their distances

I am given n points on a 2D grid. I am asked to cluster the points, where points with distance <=k for some constant k are grouped together. Note that all pairs of points within a cluster must adhere to the distance rule.
The way I thought about approaching this problem is, for each point, find its neighbors, where neighbors are defined by points that are within the distance k. We can assume each point and their initial neighbors form an initial cluster.
Then for each point, we compare its initial cluster with all of its neighbors clusters. If all of the neighbors clusters are the same as the current points cluster, then they're joined. If at least 1 of the neighbors clusters, is not the same, then all points involved and their neighbors must be disjoint.
e.g., say we have points
[0, -0.5]
[0, 0]
[0, 0.5]
[0, 1.5]
and k = 1
For point [0, -0.5] we have neighbors [0, 0] and [0, 0.5].
For point [0, 0] we have neighbors [0, 0.5] and [0, -0.5].
For point [0, 0.5] we have neighbors [0, 0], [0, -0.5], [0, 1.5].
For point [0, 1.5] we have neighbors [0, 0], [0, 1.5].
So in this case, we will have 4 clusters, defined by an individual point.
What is the most efficient way to implement this algorithm?

Ruby stepping through decimals and casting to Array produces unexpected results [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
So.. I'm trying to refactor a piece of code, namely:
v = [0.0, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 1.0]
By using the .step method. Tried this but it's giving me some odd decimals for some of the numbers. Trying to figure out why this is?
0.0.step(by: 0.1, to: 1.0).to_a
Gives me this result:
=> [0.0,
0.1,
0.2,
0.30000000000000004,
0.4,
0.5,
0.6000000000000001,
0.7000000000000001,
0.8,
0.9,
1.0]
Ruby version: 2.3.0p0
How can I go about figuring out why this is happening? Each of those numbers returns a float.
Floats are inexact. Calculations (like adding) are more exact using Rationals. If you must use floats, convert to Floats after the calculations.
p 0.step(1, 1/10r).map(&:to_f) 3 =>[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]

Graphite, averaging algorithm while displaying data

I save data every second.
Here is definition of carbon's time series:
cat /etc/carbon/storage-schemas.conf
[requests]
pattern = ^requests\.
retentions = 1s:7d
For testing purposes I send to the graphite server values of step function:
setInterval(function() {
graphite.put('step', Math.round(Math.random()) );
}, 1000);
The function produces in a random way either 1 or 0 and puts it on requests.beryllium.step target.
Here is the graphite's graph shown for 1 minute:
/render?width=400&from=-2minutes&until=-1minute&height=250&target=requests.beryllium.step&_uniq=0.06224050732088515&title=requests.beryllium.step
On the graph there are 60 data points as expected. I checked this by getting json data:
/render?width=400&from=-2minutes&until=-1minute&height=250&target=requests.beryllium.step&_uniq=0.06224050732088515&title=requests.beryllium.step&format=json
Result (60 points in the array, checked):
[{"target": "requests.beryllium.step", "datapoints": [[1.0, 1472502764], [0.0, 1472502765], [1.0, 1472502766], [0.0, 1472502767], [1.0, 1472502768], [0.0, 1472502769], [1.0, 1472502770], [0.0, 1472502771], [1.0, 1472502772], [1.0, 1472502773], [0.0, 1472502774], [1.0, 1472502775], [0.0, 1472502776], [1.0, 1472502777], [1.0, 1472502778], [0.0, 1472502779], [1.0, 1472502780], [0.0, 1472502781], [1.0, 1472502782], [1.0, 1472502783], [0.0, 1472502784], [1.0, 1472502785], [0.0, 1472502786], [1.0, 1472502787], [0.0, 1472502788], [0.0, 1472502789], [1.0, 1472502790], [1.0, 1472502791], [0.0, 1472502792], [1.0, 1472502793], [0.0, 1472502794], [1.0, 1472502795], [0.0, 1472502796], [1.0, 1472502797], [0.0, 1472502798], [1.0, 1472502799], [1.0, 1472502800], [0.0, 1472502801], [0.0, 1472502802], [0.0, 1472502803], [1.0, 1472502804], [1.0, 1472502805], [1.0, 1472502806], [1.0, 1472502807], [1.0, 1472502808], [0.0, 1472502809], [0.0, 1472502810], [1.0, 1472502811], [1.0, 1472502812], [1.0, 1472502813], [0.0, 1472502814], [1.0, 1472502815], [1.0, 1472502816], [0.0, 1472502817], [0.0, 1472502818], [0.0, 1472502819], [0.0, 1472502820], [0.0, 1472502821], [0.0, 1472502822], [1.0, 1472502823]]}]
All the data points are zeroes or ones and this is what we see on the graph, too. Till now, all is ok.
Now, I draw a graph for 15 mins:
/render?width=400&from=-16minutes&until=-1minute&height=250&target=requests.beryllium.step&_uniq=0.06224050732088515&title=requests.beryllium.step&format=png
On the graph we can see points that are not ones or zeroes. Let's check what are in the points array:
/render?width=400&from=-16minutes&until=-1minute&height=250&target=requests.beryllium.step&_uniq=0.06224050732088515&title=requests.beryllium.step&format=json
The result:
[{"target": "requests.beryllium.step", "datapoints": [[1.0, 1472502324], [0.0, 1472502325], [0.0, 1472502326], [0.0, 1472502327], [1.0, 1472502328], [1.0, 1472502329], [1.0, 1472502330], [0.0, 1472502331], [1.0, 1472502332], [1.0, 1472502333], [0.0, 1472502334], [1.0, 1472502335], [0.0, 1472502336], [0.0, 1472502337], [0.0, 1472502338], [0.0, 1472502339], [0.0, 1472502340], [1.0, 1472502341], [1.0, 1472502342], [1.0, 1472502343], [1.0, 1472502344], [0.0, 1472502345], [1.0, 1472502346], [1.0, 1472502347], [1.0, 1472502348], [1.0, 1472502349], [1.0, 1472502350], [0.0, 1472502351], [0.0, 1472502352], [1.0, 1472502353], [1.0, 1472502354], [0.0, 1472502355], [1.0, 1472502356], [1.0, 1472502357], [0.0, 1472502358], [0.0, 1472502359], [1.0, 1472502360], [1.0, 1472502361], [1.0, 1472502362], [1.0, 1472502363], [1.0, 1472502364], [0.0, 1472502365], [0.0, 1472502366], [1.0, 1472502367], [0.0, 1472502368], [0.0, 1472502369], [0.0, 1472502370], [0.0, 1472502371], [1.0, 1472502372], [1.0, 1472502373], [0.0, 1472502374], [1.0, 1472502375], [1.0, 1472502376], [0.0, 1472502377], [0.0, 1472502378], [1.0, 1472502379], [0.0, 1472502380], [0.0, 1472502381], [1.0, 1472502382], [1.0, 1472502383], [1.0, 1472502384], [1.0, 1472502385], [0.0, 1472502386], [0.0, 1472502387], [1.0, 1472502388], [1.0, 1472502389], [0.0, 1472502390], [1.0, 1472502391], [0.0, 1472502392], [1.0, 1472502393], [0.0, 1472502394], [1.0, 1472502395], [1.0, 1472502396], [1.0, 1472502397], [0.0, 1472502398], [1.0, 1472502399], [1.0, 1472502400], [0.0, 1472502401], [0.0, 1472502402], [0.0, 1472502403], [1.0, 1472502404], [1.0, 1472502405], [0.0, 1472502406], [1.0, 1472502407], [0.0, 1472502408], [1.0, 1472502409], [1.0, 1472502410], [0.0, 1472502411], [0.0, 1472502412], [0.0, 1472502413], [null, 1472502414], [0.0, 1472502415], [0.0, 1472502416], [1.0, 1472502417], [1.0, 1472502418], [1.0, 1472502419], [1.0, 1472502420], [0.0, 1472502421], [1.0, 1472502422], [1.0, 1472502423], [0.0, 1472502424], [1.0, 1472502425], [0.0, 1472502426], [1.0, 1472502427], [0.0, 1472502428], [0.0, 1472502429], [1.0, 1472502430], [1.0, 1472502431], [0.0, 1472502432], [1.0, 1472502433], [1.0, 1472502434], [0.0, 1472502435], [1.0, 1472502436], [0.0, 1472502437], [0.0, 1472502438], [1.0, 1472502439], [0.0, 1472502440], [1.0, 1472502441], [0.0, 1472502442], [1.0, 1472502443], [1.0, 1472502444], [0.0, 1472502445], [1.0, 1472502446], [0.0, 1472502447], [1.0, 1472502448], [0.0, 1472502449], [1.0, 1472502450], [0.0, 1472502451], [0.0, 1472502452], [0.0, 1472502453], [0.0, 1472502454], [0.0, 1472502455], [0.0, 1472502456], [0.0, 1472502457], [0.0, 1472502458], [1.0, 1472502459], [0.0, 1472502460], [1.0, 1472502461], [0.0, 1472502462], [1.0, 1472502463], [0.0, 1472502464], [0.0, 1472502465], [1.0, 1472502466], [0.0, 1472502467], [0.0, 1472502468], [0.0, 1472502469], [0.0, 1472502470], [0.0, 1472502471], [0.0, 1472502472], [0.0, 1472502473], [1.0, 1472502474], [1.0, 1472502475], [0.0, 1472502476], [1.0, 1472502477], [1.0, 1472502478], [1.0, 1472502479], [1.0, 1472502480], [0.0, 1472502481], [1.0, 1472502482], [0.0, 1472502483], [1.0, 1472502484], [1.0, 1472502485], [1.0, 1472502486], [1.0, 1472502487], [0.0, 1472502488], [0.0, 1472502489], [1.0, 1472502490], [1.0, 1472502491], [1.0, 1472502492], [0.0, 1472502493], [0.0, 1472502494], [1.0, 1472502495], [1.0, 1472502496], [0.0, 1472502497], [1.0, 1472502498], [1.0, 1472502499], [0.0, 1472502500], [1.0, 1472502501], [1.0, 1472502502], [0.0, 1472502503], [0.0, 1472502504], [1.0, 1472502505], [1.0, 1472502506], [1.0, 1472502507], [1.0, 1472502508], [1.0, 1472502509], [1.0, 1472502510], [1.0, 1472502511], [1.0, 1472502512], [1.0, 1472502513], [1.0, 1472502514], [0.0, 1472502515], [1.0, 1472502516], [1.0, 1472502517], [0.0, 1472502518], [0.0, 1472502519], [1.0, 1472502520], [1.0, 1472502521], [0.0, 1472502522], [1.0, 1472502523], [0.0, 1472502524], [1.0, 1472502525], [0.0, 1472502526], [1.0, 1472502527], [0.0, 1472502528], [0.0, 1472502529], [0.0, 1472502530], [0.0, 1472502531], [1.0, 1472502532], [0.0, 1472502533], [0.0, 1472502534], [0.0, 1472502535], [1.0, 1472502536], [0.0, 1472502537], [0.0, 1472502538], [1.0, 1472502539], [0.0, 1472502540], [0.0, 1472502541], [0.0, 1472502542], [1.0, 1472502543], [0.0, 1472502544], [0.0, 1472502545], [1.0, 1472502546], [1.0, 1472502547], [0.0, 1472502548], [1.0, 1472502549], [0.0, 1472502550], [1.0, 1472502551], [1.0, 1472502552], [0.0, 1472502553], [0.0, 1472502554], [1.0, 1472502555], [0.0, 1472502556], [1.0, 1472502557], [1.0, 1472502558], [0.0, 1472502559], [0.0, 1472502560], [1.0, 1472502561], [1.0, 1472502562], [1.0, 1472502563], [0.0, 1472502564], [0.0, 1472502565], [1.0, 1472502566], [1.0, 1472502567], [1.0, 1472502568], [1.0, 1472502569], [1.0, 1472502570], [0.0, 1472502571], [1.0, 1472502572], [0.0, 1472502573], [1.0, 1472502574], [1.0, 1472502575], [0.0, 1472502576], [0.0, 1472502577], [0.0, 1472502578], [0.0, 1472502579], [1.0, 1472502580], [0.0, 1472502581], [0.0, 1472502582], [0.0, 1472502583], [1.0, 1472502584], [0.0, 1472502585], [1.0, 1472502586], [0.0, 1472502587], [1.0, 1472502588], [1.0, 1472502589], [1.0, 1472502590], [0.0, 1472502591], [0.0, 1472502592], [0.0, 1472502593], [1.0, 1472502594], [0.0, 1472502595], [1.0, 1472502596], [1.0, 1472502597], [1.0, 1472502598], [0.0, 1472502599], [0.0, 1472502600], [0.0, 1472502601], [1.0, 1472502602], [0.0, 1472502603], [0.0, 1472502604], [0.0, 1472502605], [1.0, 1472502606], [1.0, 1472502607], [1.0, 1472502608], [0.0, 1472502609], [1.0, 1472502610], [1.0, 1472502611], [1.0, 1472502612], [0.0, 1472502613], [0.0, 1472502614], [0.0, 1472502615], [1.0, 1472502616], [1.0, 1472502617], [1.0, 1472502618], [0.0, 1472502619], [1.0, 1472502620], [0.0, 1472502621], [1.0, 1472502622], [0.0, 1472502623], [0.0, 1472502624], [0.0, 1472502625], [0.0, 1472502626], [0.0, 1472502627], [1.0, 1472502628], [1.0, 1472502629], [0.0, 1472502630], [0.0, 1472502631], [0.0, 1472502632], [1.0, 1472502633], [1.0, 1472502634], [0.0, 1472502635], [1.0, 1472502636], [1.0, 1472502637], [0.0, 1472502638], [1.0, 1472502639], [1.0, 1472502640], [0.0, 1472502641], [1.0, 1472502642], [1.0, 1472502643], [1.0, 1472502644], [0.0, 1472502645], [1.0, 1472502646], [1.0, 1472502647], [0.0, 1472502648], [0.0, 1472502649], [1.0, 1472502650], [1.0, 1472502651], [1.0, 1472502652], [1.0, 1472502653], [1.0, 1472502654], [1.0, 1472502655], [1.0, 1472502656], [0.0, 1472502657], [1.0, 1472502658], [1.0, 1472502659], [0.0, 1472502660], [0.0, 1472502661], [0.0, 1472502662], [0.0, 1472502663], [1.0, 1472502664], [1.0, 1472502665], [1.0, 1472502666], [1.0, 1472502667], [1.0, 1472502668], [1.0, 1472502669], [1.0, 1472502670], [0.0, 1472502671], [1.0, 1472502672], [0.0, 1472502673], [0.0, 1472502674], [0.0, 1472502675], [1.0, 1472502676], [0.0, 1472502677], [1.0, 1472502678], [1.0, 1472502679], [0.0, 1472502680], [0.0, 1472502681], [1.0, 1472502682], [0.0, 1472502683], [1.0, 1472502684], [0.0, 1472502685], [0.0, 1472502686], [0.0, 1472502687], [1.0, 1472502688], [0.0, 1472502689], [1.0, 1472502690], [0.0, 1472502691], [0.0, 1472502692], [1.0, 1472502693], [1.0, 1472502694], [0.0, 1472502695], [1.0, 1472502696], [0.0, 1472502697], [1.0, 1472502698], [0.0, 1472502699], [1.0, 1472502700], [1.0, 1472502701], [0.0, 1472502702], [0.0, 1472502703], [1.0, 1472502704], [1.0, 1472502705], [1.0, 1472502706], [0.0, 1472502707], [0.0, 1472502708], [0.0, 1472502709], [1.0, 1472502710], [0.0, 1472502711], [1.0, 1472502712], [1.0, 1472502713], [1.0, 1472502714], [1.0, 1472502715], [0.0, 1472502716], [1.0, 1472502717], [1.0, 1472502718], [0.0, 1472502719], [1.0, 1472502720], [0.0, 1472502721], [1.0, 1472502722], [1.0, 1472502723], [1.0, 1472502724], [0.0, 1472502725], [0.0, 1472502726], [0.0, 1472502727], [1.0, 1472502728], [0.0, 1472502729], [1.0, 1472502730], [0.0, 1472502731], [0.0, 1472502732], [1.0, 1472502733], [1.0, 1472502734], [0.0, 1472502735], [1.0, 1472502736], [1.0, 1472502737], [0.0, 1472502738], [0.0, 1472502739], [0.0, 1472502740], [0.0, 1472502741], [1.0, 1472502742], [0.0, 1472502743], [0.0, 1472502744], [0.0, 1472502745], [0.0, 1472502746], [0.0, 1472502747], [1.0, 1472502748], [0.0, 1472502749], [0.0, 1472502750], [1.0, 1472502751], [1.0, 1472502752], [0.0, 1472502753], [0.0, 1472502754], [0.0, 1472502755], [0.0, 1472502756], [1.0, 1472502757], [1.0, 1472502758], [1.0, 1472502759], [0.0, 1472502760], [0.0, 1472502761], [1.0, 1472502762], [1.0, 1472502763], [1.0, 1472502764], [0.0, 1472502765], [1.0, 1472502766], [0.0, 1472502767], [1.0, 1472502768], [0.0, 1472502769], [1.0, 1472502770], [0.0, 1472502771], [1.0, 1472502772], [1.0, 1472502773], [0.0, 1472502774], [1.0, 1472502775], [0.0, 1472502776], [1.0, 1472502777], [1.0, 1472502778], [0.0, 1472502779], [1.0, 1472502780], [0.0, 1472502781], [1.0, 1472502782], [1.0, 1472502783], [0.0, 1472502784], [1.0, 1472502785], [0.0, 1472502786], [1.0, 1472502787], [0.0, 1472502788], [0.0, 1472502789], [1.0, 1472502790], [1.0, 1472502791], [0.0, 1472502792], [1.0, 1472502793], [0.0, 1472502794], [1.0, 1472502795], [0.0, 1472502796], [1.0, 1472502797], [0.0, 1472502798], [1.0, 1472502799], [1.0, 1472502800], [0.0, 1472502801], [0.0, 1472502802], [0.0, 1472502803], [1.0, 1472502804], [1.0, 1472502805], [1.0, 1472502806], [1.0, 1472502807], [1.0, 1472502808], [0.0, 1472502809], [0.0, 1472502810], [1.0, 1472502811], [1.0, 1472502812], [1.0, 1472502813], [0.0, 1472502814], [1.0, 1472502815], [1.0, 1472502816], [0.0, 1472502817], [0.0, 1472502818], [0.0, 1472502819], [0.0, 1472502820], [0.0, 1472502821], [0.0, 1472502822], [1.0, 1472502823], [1.0, 1472502824], [1.0, 1472502825], [0.0, 1472502826], [0.0, 1472502827], [1.0, 1472502828], [0.0, 1472502829], [1.0, 1472502830], [0.0, 1472502831], [0.0, 1472502832], [0.0, 1472502833], [1.0, 1472502834], [0.0, 1472502835], [1.0, 1472502836], [1.0, 1472502837], [1.0, 1472502838], [1.0, 1472502839], [1.0, 1472502840], [0.0, 1472502841], [1.0, 1472502842], [1.0, 1472502843], [0.0, 1472502844], [1.0, 1472502845], [1.0, 1472502846], [0.0, 1472502847], [1.0, 1472502848], [1.0, 1472502849], [0.0, 1472502850], [1.0, 1472502851], [0.0, 1472502852], [1.0, 1472502853], [0.0, 1472502854], [0.0, 1472502855], [0.0, 1472502856], [1.0, 1472502857], [1.0, 1472502858], [0.0, 1472502859], [0.0, 1472502860], [0.0, 1472502861], [0.0, 1472502862], [1.0, 1472502863], [1.0, 1472502864], [0.0, 1472502865], [1.0, 1472502866], [0.0, 1472502867], [1.0, 1472502868], [0.0, 1472502869], [0.0, 1472502870], [1.0, 1472502871], [1.0, 1472502872], [0.0, 1472502873], [0.0, 1472502874], [0.0, 1472502875], [1.0, 1472502876], [1.0, 1472502877], [1.0, 1472502878], [1.0, 1472502879], [0.0, 1472502880], [0.0, 1472502881], [0.0, 1472502882], [0.0, 1472502883], [0.0, 1472502884], [0.0, 1472502885], [1.0, 1472502886], [1.0, 1472502887], [1.0, 1472502888], [1.0, 1472502889], [1.0, 1472502890], [0.0, 1472502891], [0.0, 1472502892], [1.0, 1472502893], [1.0, 1472502894], [0.0, 1472502895], [1.0, 1472502896], [1.0, 1472502897], [0.0, 1472502898], [0.0, 1472502899], [1.0, 1472502900], [0.0, 1472502901], [0.0, 1472502902], [0.0, 1472502903], [0.0, 1472502904], [1.0, 1472502905], [0.0, 1472502906], [0.0, 1472502907], [1.0, 1472502908], [0.0, 1472502909], [1.0, 1472502910], [0.0, 1472502911], [0.0, 1472502912], [0.0, 1472502913], [1.0, 1472502914], [0.0, 1472502915], [1.0, 1472502916], [1.0, 1472502917], [0.0, 1472502918], [1.0, 1472502919], [0.0, 1472502920], [0.0, 1472502921], [1.0, 1472502922], [1.0, 1472502923], [1.0, 1472502924], [1.0, 1472502925], [1.0, 1472502926], [0.0, 1472502927], [0.0, 1472502928], [1.0, 1472502929], [1.0, 1472502930], [0.0, 1472502931], [1.0, 1472502932], [1.0, 1472502933], [1.0, 1472502934], [1.0, 1472502935], [0.0, 1472502936], [0.0, 1472502937], [1.0, 1472502938], [0.0, 1472502939], [0.0, 1472502940], [0.0, 1472502941], [1.0, 1472502942], [1.0, 1472502943], [0.0, 1472502944], [1.0, 1472502945], [0.0, 1472502946], [1.0, 1472502947], [0.0, 1472502948], [0.0, 1472502949], [0.0, 1472502950], [1.0, 1472502951], [1.0, 1472502952], [1.0, 1472502953], [0.0, 1472502954], [0.0, 1472502955], [1.0, 1472502956], [0.0, 1472502957], [0.0, 1472502958], [1.0, 1472502959], [0.0, 1472502960], [1.0, 1472502961], [0.0, 1472502962], [1.0, 1472502963], [1.0, 1472502964], [0.0, 1472502965], [0.0, 1472502966], [0.0, 1472502967], [0.0, 1472502968], [0.0, 1472502969], [0.0, 1472502970], [0.0, 1472502971], [0.0, 1472502972], [1.0, 1472502973], [1.0, 1472502974], [1.0, 1472502975], [1.0, 1472502976], [0.0, 1472502977], [0.0, 1472502978], [0.0, 1472502979], [0.0, 1472502980], [1.0, 1472502981], [1.0, 1472502982], [0.0, 1472502983], [0.0, 1472502984], [1.0, 1472502985], [0.0, 1472502986], [0.0, 1472502987], [1.0, 1472502988], [0.0, 1472502989], [1.0, 1472502990], [1.0, 1472502991], [1.0, 1472502992], [1.0, 1472502993], [0.0, 1472502994], [0.0, 1472502995], [1.0, 1472502996], [1.0, 1472502997], [1.0, 1472502998], [1.0, 1472502999], [0.0, 1472503000], [1.0, 1472503001], [1.0, 1472503002], [0.0, 1472503003], [1.0, 1472503004], [0.0, 1472503005], [0.0, 1472503006], [1.0, 1472503007], [1.0, 1472503008], [0.0, 1472503009], [0.0, 1472503010], [0.0, 1472503011], [0.0, 1472503012], [0.0, 1472503013], [1.0, 1472503014], [1.0, 1472503015], [1.0, 1472503016], [0.0, 1472503017], [0.0, 1472503018], [1.0, 1472503019], [0.0, 1472503020], [0.0, 1472503021], [0.0, 1472503022], [1.0, 1472503023], [1.0, 1472503024], [0.0, 1472503025], [0.0, 1472503026], [0.0, 1472503027], [0.0, 1472503028], [0.0, 1472503029], [1.0, 1472503030], [0.0, 1472503031], [0.0, 1472503032], [0.0, 1472503033], [0.0, 1472503034], [0.0, 1472503035], [1.0, 1472503036], [1.0, 1472503037], [1.0, 1472503038], [1.0, 1472503039], [0.0, 1472503040], [1.0, 1472503041], [0.0, 1472503042], [0.0, 1472503043], [1.0, 1472503044], [1.0, 1472503045], [1.0, 1472503046], [1.0, 1472503047], [0.0, 1472503048], [0.0, 1472503049], [1.0, 1472503050], [0.0, 1472503051], [1.0, 1472503052], [0.0, 1472503053], [1.0, 1472503054], [0.0, 1472503055], [1.0, 1472503056], [0.0, 1472503057], [1.0, 1472503058], [1.0, 1472503059], [0.0, 1472503060], [1.0, 1472503061], [1.0, 1472503062], [0.0, 1472503063], [0.0, 1472503064], [1.0, 1472503065], [0.0, 1472503066], [1.0, 1472503067], [1.0, 1472503068], [1.0, 1472503069], [0.0, 1472503070], [0.0, 1472503071], [0.0, 1472503072], [0.0, 1472503073], [1.0, 1472503074], [1.0, 1472503075], [1.0, 1472503076], [0.0, 1472503077], [1.0, 1472503078], [1.0, 1472503079], [1.0, 1472503080], [0.0, 1472503081], [0.0, 1472503082], [1.0, 1472503083], [1.0, 1472503084], [0.0, 1472503085], [1.0, 1472503086], [0.0, 1472503087], [0.0, 1472503088], [1.0, 1472503089], [1.0, 1472503090], [0.0, 1472503091], [1.0, 1472503092], [0.0, 1472503093], [1.0, 1472503094], [0.0, 1472503095], [1.0, 1472503096], [0.0, 1472503097], [1.0, 1472503098], [1.0, 1472503099], [0.0, 1472503100], [1.0, 1472503101], [0.0, 1472503102], [0.0, 1472503103], [1.0, 1472503104], [0.0, 1472503105], [1.0, 1472503106], [0.0, 1472503107], [1.0, 1472503108], [0.0, 1472503109], [1.0, 1472503110], [0.0, 1472503111], [0.0, 1472503112], [1.0, 1472503113], [0.0, 1472503114], [0.0, 1472503115], [1.0, 1472503116], [0.0, 1472503117], [0.0, 1472503118], [1.0, 1472503119], [1.0, 1472503120], [0.0, 1472503121], [1.0, 1472503122], [1.0, 1472503123], [0.0, 1472503124], [0.0, 1472503125], [1.0, 1472503126], [1.0, 1472503127], [0.0, 1472503128], [0.0, 1472503129], [1.0, 1472503130], [1.0, 1472503131], [0.0, 1472503132], [1.0, 1472503133], [0.0, 1472503134], [1.0, 1472503135], [1.0, 1472503136], [1.0, 1472503137], [1.0, 1472503138], [0.0, 1472503139], [1.0, 1472503140], [0.0, 1472503141], [0.0, 1472503142], [0.0, 1472503143], [0.0, 1472503144], [0.0, 1472503145], [1.0, 1472503146], [0.0, 1472503147], [1.0, 1472503148], [1.0, 1472503149], [0.0, 1472503150], [1.0, 1472503151], [0.0, 1472503152], [0.0, 1472503153], [0.0, 1472503154], [0.0, 1472503155], [1.0, 1472503156], [1.0, 1472503157], [1.0, 1472503158], [0.0, 1472503159], [1.0, 1472503160], [1.0, 1472503161], [1.0, 1472503162], [1.0, 1472503163], [1.0, 1472503164], [0.0, 1472503165], [1.0, 1472503166], [0.0, 1472503167], [0.0, 1472503168], [1.0, 1472503169], [0.0, 1472503170], [0.0, 1472503171], [0.0, 1472503172], [1.0, 1472503173], [0.0, 1472503174], [1.0, 1472503175], [1.0, 1472503176], [1.0, 1472503177], [1.0, 1472503178], [0.0, 1472503179], [1.0, 1472503180], [1.0, 1472503181], [0.0, 1472503182], [0.0, 1472503183], [1.0, 1472503184], [1.0, 1472503185], [0.0, 1472503186], [1.0, 1472503187], [0.0, 1472503188], [1.0, 1472503189], [1.0, 1472503190], [1.0, 1472503191], [0.0, 1472503192], [0.0, 1472503193], [0.0, 1472503194], [1.0, 1472503195], [0.0, 1472503196], [0.0, 1472503197], [0.0, 1472503198], [0.0, 1472503199], [0.0, 1472503200], [1.0, 1472503201], [0.0, 1472503202], [0.0, 1472503203], [1.0, 1472503204], [0.0, 1472503205], [1.0, 1472503206], [0.0, 1472503207], [1.0, 1472503208], [0.0, 1472503209], [0.0, 1472503210], [0.0, 1472503211], [0.0, 1472503212], [0.0, 1472503213], [0.0, 1472503214], [0.0, 1472503215], [1.0, 1472503216], [0.0, 1472503217], [1.0, 1472503218], [0.0, 1472503219], [0.0, 1472503220], [0.0, 1472503221], [1.0, 1472503222], [1.0, 1472503223]]}]
There are 900 points (15*60). Again, we can see that the data points are zeroes or ones only.
Therefore, there is some averaging algorithm that displays data on the graph which shows points that are not zeroes or ones.
I'm interested to know what calculation graphite does to draw graphs when I change time window from 1 min to some wider range.
Following the source code, here is the part responsible for calculation of how many points per pixel (best case 1 on 1):
def consolidateDataPoints(self):
numberOfPixels = self.graphWidth = self.area['xmax'] - self.area['xmin'] - (self.lineWidth + 1)
for series in self.data:
numberOfDataPoints = self.timeRange/series.step
minXStep = float( self.params.get('minXStep',1.0) )
divisor = self.timeRange / series.step
bestXStep = numberOfPixels / divisor
if bestXStep < minXStep:
drawableDataPoints = int( numberOfPixels / minXStep )
pointsPerPixel = math.ceil( float(numberOfDataPoints) / float(drawableDataPoints) )
series.consolidate(pointsPerPixel)
series.xStep = (numberOfPixels * pointsPerPixel) / numberOfDataPoints
else:
series.xStep = bestXStep
As you may note - adding minXStep=NUMBER to the query string can extend consolidation window. The default consolidation method is average.

Rounding errors in Ruby matrix implementation

I'm doing a bit 'o matrix algebra in ruby. When testing the results, I'm seeing what I can only assume is a rounding error.
All I'm doing is multiplying 3 matrices, but the values are fairly small:
c_xy:
[0.9702957262759965, 0.012661213742314235, -0.24159035004964077]
[0, 0.9986295347545738, 0.05233595624294383]
[0.24192189559966773, -0.050781354673095955, 0.9689659697053497]
i2k = Matrix[[8.1144E-06, 0.0, 0.0],
[0.0, 8.1144E-06, 0.0],
[0.0, 0.0, 8.1144E-06]]
c_yx:
[0.9702957262759965, 0, 0.24192189559966773]
[0.012661213742314235, 0.9986295347545738, -0.050781354673095955]
[-0.24159035004964077, 0.05233595624294383, 0.9689659697053497]
What I'm trying to do is c_xy * i2k * c_yx. Here's what I expect (this was done in Excel):
8.1144E-06 0 2.11758E-22
0 8.1144E-06 0
2.11758E-22 -5.29396E-23 8.1144E-06
And what I get:
[8.1144e-06, 1.3234889800848443e-23, 6.352747104407253e-22]
[0.0, 8.114399999999998e-06, -5.293955920339377e-23]
[2.117582368135751e-22, 0.0, 8.1144e-06]
As you can see, the first column matches, as does the diagonal. But then (in r,c indexing) (0,1) is wrong (though approaching 0), (0,2) is very wrong, and (1,2) and (2,1) seem to be transposed. I thought it had something to do with the8.1144e-6 value, and tried wrapping it in a BigDecimal to no avail.
Any ideas on places I can look? I'm using the standard Ruby Matrix library
edit
here's the code.
phi1 = 0.24434609527920614
phi2 = 0.05235987755982988
i2k = Matrix[[8.1144E-06, 0.0, 0.0],
[0.0, 8.1144E-06, 0.0],
[0.0, 0.0, 8.1144E-06]]
c_x = Matrix[[1, 0, 0],
[0, Math.cos(phi2), Math.sin(phi2)],
[0, -Math.sin(phi2), Math.cos(phi2)]]
c_y = Matrix[[Math.cos(phi1), 0, -Math.sin(phi1)],
[0, 1, 0],
[Math.sin(phi1), 0, Math.cos(phi1)]]
c_xy = c_y * c_x
c_yx = c_xy.transpose
c_xy * i2k * c_yx
i2k is equal to the identity matrix times 8.1144E-06. This simplifies the answer to:
c_xy * i2k * c_yx = 8.1144E-06 * c_xy * c_yx
However since c_yx = c_xy.transpose and c_xy is a rotation matrix, the transpose of any rotation matrix is its inverse. So c_xy * c_yx is the identity matrix, and thus the exact answer is 8.1144E-06 times the identity matrix.
Here is one way to calculate c_xy * c_yx without using the matrix algebra a priori:
require 'matrix'
require 'pp'
phi1 = 14 * Math::PI/180
phi2 = 3 * Math::PI/180
c_x = Matrix[
[1,0,0],
[0, Math.cos(phi2), Math.sin(phi2) ],
[0, -Math.sin(phi2), Math.cos(phi2) ] ]
c_y = Matrix[
[Math.cos(phi1), 0, -Math.sin(phi1) ],
[0,1,0],
[Math.sin(phi1), 0, Math.cos(phi1) ] ]
c_xy = c_y * c_x
c_yx = c_xy.transpose
product = c_xy * c_yx
pp *product
clone = *product
puts "\nApplying EPSILON:"
product.each_with_index do |e,i,j|
clone[i][j] = 0 if e.abs <= Float::EPSILON
end
pp clone
Output:
[1.0, 0.0, 2.7755575615628914e-17]
[0.0, 0.9999999999999999, -6.938893903907228e-18]
[2.7755575615628914e-17, -6.938893903907228e-18, 0.9999999999999999]
Applying EPSILON:
[1.0, 0, 0]
[0, 0.9999999999999999, 0]
[0, 0, 0.9999999999999999]
which one can then surmise should be the identity matrix. This uses Float::EPSILON which is about 2.220446049250313e-16 in order to set values that have an absolute value no more than this equal to 0. These kinds of approximations are inevitable in floating point calculations; one must evaluate the appropriateness of these approximations on a case-by-case basis.
An alternative is to do symbolic computation where possible rather than numeric.
Floating point numbers have a precision:
puts Float::DIG # => 15
That's the number of decimal digits a Float can have on my, and probably your system. Numbers smaller than 1E-15 can not be represented with a float. You could try BigDecimal for arbitrary large precision.

2d projection of Graphics3D object

I have a Graphics3D object. I want to export it as a bitmap, and to calculate the bitmap-pixel coordinates of certain 3D points.
Mathematica obviously does a projection from 3D objects to 2D pixel coordinates when it draws the 3D graphic. How can I find out what this projection is?
I'd rather avoid doing lots of tricky geometrical calculations based on ViewVector and ViewAngle and ImageSize and BoundingBox. Is there a shortcut?
Damon.
You could GeometricTransform using the option "Transformation" -> "Perspective". Suppose your projected chess board looks something like this
img = Image#
Plot3D[0, {x, -1, 1}, {y, -1, 1}, Mesh -> 7,
MeshShading -> {{Black, White}, {White, Black}}, Boxed -> False,
AxesEdge -> {{-1, -1}, {-1, -1}, None}, AxesOrigin -> {-1, -1, 0}]
To find the projection you will need the coordinates of at least 4 control points in img for which you know the {x,y}-coordinates. There probably are methods to have Mathematica find these coordinates automatically but you can select them manually by right-clicking on img and choosing "Get Coordinates". Click on the control points of your choice (in this case I chose the 4 corners of the chessboard) and
copy/paste their coordinates to a new line. You should get something like
controls = {{13.5`, 151.5`}, {235.5`, 68.5`},
{332.5`, 206.5`}, {139.5`, 262.5`}};
The projection function and matrix then become
transform = FindGeometricTransform[controls,
{{0, 0}, {8, 0}, {8, 8}, {0, 8}},
"Transformation" -> "Perspective"][[2]]
transfMat = TranformationMatrix[transform]
Note that I chose the chessboard to be an 8x8 square centred at {4,4}, but you can choose any square.
A point {x,y} on the chessboard will now correspond to the point in img with pixel coordinates transform[{x,y}] or, using the projection matrix, (transfMat[[{1,2}]].{x,y,1})/(transfMat[[3]].{x,y,1}). So for example, to put a marker on D6, which would be at position {x,y}={4-1/2,6-1/2} in my 8x8 square, you could do something like
ImageCompose[img, Image[BoxMatrix[2]], Round[transform[{4 - 1/2, 6 - 1/2}]]]
When you render your Graphics3D object, you can specify the ViewMatrix option. You can set the transformation (such as a rotation) and then the projection.
This way you can use your explicit knowledge of the projection used to calculate the correct planar coordinates.

Resources