Ruby, 150 145 137 127 125 106 88 76 bytes
76 bytes
->(x,y,l){x,y,t=x.abs,y.abs,3**0.5;d=t*l;z=d-t*x-y;2*y>d ?-1:2*x<l ?1:z<=>0}
Changed triple comparison to a rocket.
88 bytes
->(x,y,l){x,y,t=x.abs,y.abs,3**0.5;d=t*l;z=d-t*x-y;2*y>d ?-1:2*x<l ?1:z==0 ?0:0<z ?1:-1}
Remove the y equal to apothem test for points on the hexagon, because for integers, that can never be true.
106 bytes:
->(x,y,l){x,y,t=x.abs,y.abs,3**0.5;d=t*l;z=d-t*x-y;2*y==d&&2*x<=l ?0:2*y>d ?-1:2*x<l ?1:z==0 ?0:0<z ?1:-1}
Poster suggested not using epsilon, so replaced epsilon with zero and rearranged, removed an abs, etc.
125 bytes:
->(x,y,l){x,y,t,e=x.abs,y.abs,3**0.5,1e-9;d=t*l;z=d-t*x-y;(2*y-d).abs<=e&&2*x<=l ?0:2*y>d ?-1:2*x<l ?1:z.abs<=e ?0:0<z ?1:-1}
Incorporate y into definition of z and remove some parentheses.
127 bytes:
->(x,y,l){x,y,t,e=x.abs,y.abs,3**0.5,1e-9;d=t*l;z=d-t*x;(2*y-d).abs<=e&&2*x<=l ?0:2*y>d ?-1:2*x<l ?1:(z-y).abs<=e ?0:y<z ?1:-1}
Rearranged terms to avoid necessity of to_f cast. Use d (double the apothem) instead of a (the apothem). Combine multiple assignments.
137 bytes:
->(x,y,l){x=x.abs.to_f;y=y.abs.to_f;a=3**0.5*l/2;e=1e-9;z=2*a*(1-x/l);(y-a).abs<=e&&2*x<=l ?0:y>a ?-1:2*x<l ?1:(z-y).abs<=e ?0:y<z ?1:-1}
Inlined 'c'.
150 bytes:
->(x,y,l){c=l/2.0;x=x.abs.to_f;y=y.abs.to_f;a=3**0.5*l/2;e=1e-10;z=2*a*(1-x/l);(y-a).abs<=e&&x<=c ?0:(y>a ?-1:(x<c ?1:((z-y).abs<=e ?0:(y<z ?1:-1))))}
This works for integers or floats! The epsilon test is so that points within round off error of being on the edge are correctly identified.
The absolute values move everything into quadrant one.
The value 'a' is the apothem distance (the y-intercept of the hexagon).
The value 'c' is the x-value of the upper right corner of the hexagon.
The value 'z' is to see if the point is above or below the slant line from the corner to the x-intercept.
Ungolfed:
hex = ->(x,y,l){
c = l/2.0;
x = x.abs.to_f;
y = y.abs.to_f;
a = 3**0.5 * l / 2;
e = 1e-10;
z = 2*a*(1 - x/l);
if (y-a).abs <= e && x <= c then 0
elsif (y>a) then -1
elsif (x<c) then 1
elsif (z-y).abs <= e then 0
elsif y < z then 1
else -1
end
}
Test
hex = ->(x,y,l){x,y,t=x.abs,y.abs,3**0.5;d=t*l;z=d-t*x-y;2*y>d ?-1:2*x<l ?1:z<=>0}
cases = [
[0,0,1,1],
[0,1,1,-1],
[0,-1,1,-1],
[1,0,1,0],
[-1,0,1,0],
[-1,-1,1,-1],
[1,1,1,-1],
[-2,-3,4,1],
[32,45,58,1],
[99,97,155,-1],
[123,135,201,1]
]
cases.each { |test|
expected = test[3]
actual = hex.call(test[0],test[1],test[2])
status = expected == actual ? "PASS" : "FAIL";
p "#{status}. #(x,y) L = (#{test[0]},#{test[1]}) #{test[2]} Expects #{expected}. Actual #{actual}"
}
"Done!"
I assume this is a regular hexagon, but you should make that explicit. – Level River St – 2016-03-10T21:53:57.317
@LevelRiverSt yes. A regular. I'll add that in a moment. – agtoever – 2016-03-10T22:02:59.880
1Can we take x,y as a complex number x+yi? – lirtosiast – 2016-03-10T22:41:17.923
Related. – xnor – 2016-03-11T01:48:37.780
@lirtosiast the question is about a hexagon in the euclidian plane, not in the complex plane. Because of that complex input isn't allowed. – agtoever – 2016-03-11T08:18:57.427
@agtoever There is usually no need to specify a certain value range that we have to support. Going with something like the integer limit of the chosen language is enough most of the time. – Denker – 2016-03-11T15:37:32.227
The input range should be what can reasonably handled with the language of choice - see this meta post http://meta.codegolf.stackexchange.com/q/8471/21348 (ok already done. Thank you)
– edc65 – 2016-03-11T15:41:20.037