• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 15:55
CEST 21:55
KST 04:55
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
TL.net Map Contest #21: Voting9[ASL20] Ro4 Preview: Descent11Team TLMC #5: Winners Announced!3[ASL20] Ro8 Preview Pt2: Holding On9Maestros of the Game: Live Finals Preview (RO4)5
Community News
BSL Team A vs Koreans - Sat-Sun 16:00 CET4Weekly Cups (Oct 6-12): Four star herO85.0.15 Patch Balance Hotfix (2025-10-8)80Weekly Cups (Sept 29-Oct 5): MaxPax triples up3PartinG joins SteamerZone, returns to SC2 competition32
StarCraft 2
General
Stellar Fest: StarCraft II returns to Canada The New Patch Killed Mech! herO Talks: Poor Performance at EWC and more... TL.net Map Contest #21: Voting Revisiting the game after10 years and wow it's bad
Tourneys
SC2's Safe House 2 - October 18 & 19 $1,200 WardiTV October (Oct 21st-31st) WardiTV Mondays RSL Offline Finals Dates + Ticket Sales! SC4ALL $6,000 Open LAN in Philadelphia
Strategy
Custom Maps
External Content
Mutation # 495 Rest In Peace Mutation # 494 Unstable Environment Mutation # 493 Quick Killers Mutation # 492 Get Out More
Brood War
General
BW General Discussion BSL Team A vs Koreans - Sat-Sun 16:00 CET Question regarding recent ASL Bisu vs Larva game [Interview] Grrrr... 2024 Pros React To: BarrackS + FlaSh Coaching vs SnOw
Tourneys
[ASL20] Semifinal B SC4ALL $1,500 Open Bracket LAN [Megathread] Daily Proleagues [ASL20] Semifinal A
Strategy
BW - ajfirecracker Strategy & Training Relatively freeroll strategies Current Meta Siegecraft - a new perspective
Other Games
General Games
Stormgate/Frost Giant Megathread Dawn of War IV Path of Exile Nintendo Switch Thread ZeroSpace Megathread
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
SPIRED by.ASL Mafia {211640} TL Mafia Community Thread
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine Men's Fashion Thread Sex and weight loss
Fan Clubs
The herO Fan Club! The Happy Fan Club!
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Series you have seen recently... Movie Discussion!
Sports
Formula 1 Discussion 2024 - 2026 Football Thread MLB/Baseball 2023 NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Recent Gifted Posts
Blogs
The Heroism of Pepe the Fro…
Peanutsc
Rocket League: Traits, Abili…
TrAiDoS
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1562 users

The Big Programming Thread - Page 661

Forum Index > General Forum
Post a Reply
Prev 1 659 660 661 662 663 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Manit0u
Profile Blog Joined August 2004
Poland17384 Posts
September 03 2015 11:42 GMT
#13201
I'm no good with trigonometry. Could you help me out with Haversine formula a bit?

I have this query to get locations in select radius range from target location. Could it be made simpler? It works but it looks horrible...


$query = $queryBuilder
->select('CAST((2 * ATAN2(SQRT(POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude))), SQRT(1 - POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude)))) * 6371) AS DECIMAL(65,10)) AS d')
->having($expr->lte('d', ':radius'))
->setParameter('radius', $radius)
->getQuery();
Time is precious. Waste it wisely.
mca64Launcher_
Profile Joined June 2015
Poland629 Posts
Last Edited: 2015-09-03 11:46:50
September 03 2015 11:46 GMT
#13202
who cares how it looks. Its works! Its all you need.
Manit0u
Profile Blog Joined August 2004
Poland17384 Posts
September 03 2015 13:12 GMT
#13203
Yeah, but I'm afraid of complicated maths affecting database performance when requests (and thus queries) start to ramp up.
Time is precious. Waste it wisely.
Acrofales
Profile Joined August 2010
Spain18093 Posts
Last Edited: 2015-09-03 13:59:08
September 03 2015 13:58 GMT
#13204
On September 03 2015 22:12 Manit0u wrote:
Yeah, but I'm afraid of complicated maths affecting database performance when requests (and thus queries) start to ramp up.

So do it in your PHP instead of SQL. I have no idea what is going to be faster. I would, as a general principal, expect PHP to do trigonometry faster than SQL, although you never knows until you test.
Manit0u
Profile Blog Joined August 2004
Poland17384 Posts
Last Edited: 2015-09-03 14:17:51
September 03 2015 14:17 GMT
#13205
I'd have to grab all rows from db (at least lat and long values for each row) and then do the calculations for each one of them. Sounds simple enough but then you have to do all the circus with having a collection, performing calculations for each element, removing elements that didn't match the criteria and returning the collection.

I need to think about it.
Time is precious. Waste it wisely.
Cyx.
Profile Joined November 2010
Canada806 Posts
September 03 2015 14:29 GMT
#13206
On September 03 2015 22:58 Acrofales wrote:
Show nested quote +
On September 03 2015 22:12 Manit0u wrote:
Yeah, but I'm afraid of complicated maths affecting database performance when requests (and thus queries) start to ramp up.

So do it in your PHP instead of SQL. I have no idea what is going to be faster. I would, as a general principal, expect PHP to do trigonometry faster than SQL, although you never knows until you test.

I feel like I would expect SQL to do it faster - I can't see SQL being *slow* at trig, and I feel like the overhead of selecting the ones you need in PHP would more than outweigh the cost of using SQL trig. I have literally no idea though.
Manit0u
Profile Blog Joined August 2004
Poland17384 Posts
September 03 2015 14:50 GMT
#13207
I'll test performance of SQL computations versus this thing:


public function getOfficesInRadius($latitude, $longitude, $radius)
{
$geolocations = $this->findAll();

foreach ($geolocations as $key => $location) {
$distance = $this->haversineGreatCircleDistance($latitude, $longitude, $location->getLatitude(), $location->getLongitude());

if ($distance > $radius) {
unset($geolocations[$key]);
}
}

return $geolocations;
}

public function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo)
{
// degrees to radians
$latitudeFrom = deg2rad($latitudeFrom);
$latitudeTo = deg2rad($latitudeTo);
$longitudeFrom = deg2rad($longitudeFrom);
$longitudeTo = deg2rad($longitudeTo);

$latitudeDelta = $latitudeTo - $latitudeFrom;
$longitudeDelta = $longitudeTo - $longitudeFrom;

$angle = 2 * asin(sqrt(pow(sin($latitudeDelta / 2), 2) + cos($latitudeFrom) * cos($latitudeTo) * pow(sin($longitudeDelta / 2), 2)));

return $angle * self::EARTH_RADIUS;
}
Time is precious. Waste it wisely.
Acrofales
Profile Joined August 2010
Spain18093 Posts
Last Edited: 2015-09-03 15:06:45
September 03 2015 14:56 GMT
#13208
On September 03 2015 23:29 Cyx. wrote:
Show nested quote +
On September 03 2015 22:58 Acrofales wrote:
On September 03 2015 22:12 Manit0u wrote:
Yeah, but I'm afraid of complicated maths affecting database performance when requests (and thus queries) start to ramp up.

So do it in your PHP instead of SQL. I have no idea what is going to be faster. I would, as a general principal, expect PHP to do trigonometry faster than SQL, although you never knows until you test.

I feel like I would expect SQL to do it faster - I can't see SQL being *slow* at trig, and I feel like the overhead of selecting the ones you need in PHP would more than outweigh the cost of using SQL trig. I have literally no idea though.

Yeah, but the exerpt had no where clause, so I (mistakenly) assumed he simply wanted all distances. If there's more to the query then the only way to see speedups is to exhaustively test it.

Other than that, the only way I can think of making that faster is by declaring a MySQL procedure that does the calculation. You should then be able to set a variable to compute:
POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude))

and store it in a local variable. That way you don't have to compute it twice in the query. However, I don't know what the overhead is of doing that kinda stuff in MySQL.


EDIT: I was going to suggest using cosine law instead of haversine if your distances are large enough, but I checked, and float precision in MySQL is actually quite shockingly low. If precision is important, stick with Haversine. If precision ISN'T important, and a rough estimate is okay, then you can use pythagoras for most stuff unless you are computing distances at very high latitudes. Here's an overview of your options (and the formula for calculation): http://www.movable-type.co.uk/scripts/latlong.html
Acrofales
Profile Joined August 2010
Spain18093 Posts
September 03 2015 15:43 GMT
#13209
On September 03 2015 23:50 Manit0u wrote:
I'll test performance of SQL computations versus this thing:


public function getOfficesInRadius($latitude, $longitude, $radius)
{
$geolocations = $this->findAll();

foreach ($geolocations as $key => $location) {
$distance = $this->haversineGreatCircleDistance($latitude, $longitude, $location->getLatitude(), $location->getLongitude());

if ($distance > $radius) {
unset($geolocations[$key]);
}
}

return $geolocations;
}

public function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo)
{
// degrees to radians
$latitudeFrom = deg2rad($latitudeFrom);
$latitudeTo = deg2rad($latitudeTo);
$longitudeFrom = deg2rad($longitudeFrom);
$longitudeTo = deg2rad($longitudeTo);

$latitudeDelta = $latitudeTo - $latitudeFrom;
$longitudeDelta = $longitudeTo - $longitudeFrom;

$angle = 2 * asin(sqrt(pow(sin($latitudeDelta / 2), 2) + cos($latitudeFrom) * cos($latitudeTo) * pow(sin($longitudeDelta / 2), 2)));

return $angle * self::EARTH_RADIUS;
}


By taking the arcsin rather than the arctan you run into problems with precision for small distances (somewhere around a meter according to stackexchange). If that's not an issue, carry on. If it is, use the arctan instead:


$a = pow(sin($latitudeDelta / 2), 2) + cos($latitudeFrom) * cos($latitudeTo) * pow(sin($longitudeDelta / 2), 2);
return 2 * atan2(sqrt($a), sqrt(1 - $a)) * self::EARTH_RADIUS;


Or something.
Manit0u
Profile Blog Joined August 2004
Poland17384 Posts
September 03 2015 18:30 GMT
#13210
On September 03 2015 23:56 Acrofales wrote:
Yeah, but the exerpt had no where clause, so I (mistakenly) assumed he simply wanted all distances. If there's more to the query then the only way to see speedups is to exhaustively test it.


I kind of forgot to explain the query and blindly posted the DQL.

What it's supposed to do is to return all rows that match the criteria (all locations whose distance from target is equal to or less than given radius in km).

Precision isn't super important (it's used to display all shops/offices whatever within a certain radius).

I did some testing and PHP won't be good for that. Even though you can split it into chunks that are more understandable and easier to debug you run into real trouble when you're trying to parse an array containing thousands of objects.
Time is precious. Waste it wisely.
Acrofales
Profile Joined August 2010
Spain18093 Posts
Last Edited: 2015-09-03 18:54:27
September 03 2015 18:53 GMT
#13211
On September 04 2015 03:30 Manit0u wrote:
Show nested quote +
On September 03 2015 23:56 Acrofales wrote:
Yeah, but the exerpt had no where clause, so I (mistakenly) assumed he simply wanted all distances. If there's more to the query then the only way to see speedups is to exhaustively test it.


I kind of forgot to explain the query and blindly posted the DQL.

What it's supposed to do is to return all rows that match the criteria (all locations whose distance from target is equal to or less than given radius in km).

Precision isn't super important (it's used to display all shops/offices whatever within a certain radius).

I did some testing and PHP won't be good for that. Even though you can split it into chunks that are more understandable and easier to debug you run into real trouble when you're trying to parse an array containing thousands of objects.

Any chance you can use Google Services and just geofence it? :D

This kind of thing is better done with a dedicated GIS system than an SQL backend.

Anyway, if approximate results is enough, use pythagoras. Way simpler and faster.
Manit0u
Profile Blog Joined August 2004
Poland17384 Posts
September 03 2015 20:31 GMT
#13212
On September 04 2015 03:53 Acrofales wrote:
Show nested quote +
On September 04 2015 03:30 Manit0u wrote:
On September 03 2015 23:56 Acrofales wrote:
Yeah, but the exerpt had no where clause, so I (mistakenly) assumed he simply wanted all distances. If there's more to the query then the only way to see speedups is to exhaustively test it.


I kind of forgot to explain the query and blindly posted the DQL.

What it's supposed to do is to return all rows that match the criteria (all locations whose distance from target is equal to or less than given radius in km).

Precision isn't super important (it's used to display all shops/offices whatever within a certain radius).

I did some testing and PHP won't be good for that. Even though you can split it into chunks that are more understandable and easier to debug you run into real trouble when you're trying to parse an array containing thousands of objects.

Any chance you can use Google Services and just geofence it? :D

This kind of thing is better done with a dedicated GIS system than an SQL backend.

Anyway, if approximate results is enough, use pythagoras. Way simpler and faster.


Ehh... It's just a client who wants to add stores/whatever to the db. Put in the address, grab lat/long from google and people can then search for nearby stores on his website.
Time is precious. Waste it wisely.
delHospital
Profile Blog Joined December 2010
Poland261 Posts
September 03 2015 21:48 GMT
#13213
On September 03 2015 20:42 Manit0u wrote:
I'm no good with trigonometry. Could you help me out with Haversine formula a bit?

I have this query to get locations in select radius range from target location. Could it be made simpler? It works but it looks horrible...

+ Show Spoiler +

$query = $queryBuilder
->select('CAST((2 * ATAN2(SQRT(POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude))), SQRT(1 - POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude)))) * 6371) AS DECIMAL(65,10)) AS d')
->having($expr->lte('d', ':radius'))
->setParameter('radius', $radius)
->getQuery();

Using a WITH clause (or something like that) to assign RADIANS($latitude) and RADIANS($longitude) to variables would probably help a bit. Also, can't you pass these variables in a safer way (that is, without string concatenation)?
Manit0u
Profile Blog Joined August 2004
Poland17384 Posts
September 03 2015 23:30 GMT
#13214
On September 04 2015 06:48 delHospital wrote:
Show nested quote +
On September 03 2015 20:42 Manit0u wrote:
I'm no good with trigonometry. Could you help me out with Haversine formula a bit?

I have this query to get locations in select radius range from target location. Could it be made simpler? It works but it looks horrible...

+ Show Spoiler +

$query = $queryBuilder
->select('CAST((2 * ATAN2(SQRT(POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude))), SQRT(1 - POW(SIN(RADIANS(latitude - '
.$latitude
.') / 2) , 2) + POW(SIN(RADIANS(longitude - '
.$longitude
.') / 2) , 2) * COS(RADIANS('
.$latitude
.')) * COS(RADIANS(latitude)))) * 6371) AS DECIMAL(65,10)) AS d')
->having($expr->lte('d', ':radius'))
->setParameter('radius', $radius)
->getQuery();

Using a WITH clause (or something like that) to assign RADIANS($latitude) and RADIANS($longitude) to variables would probably help a bit. Also, can't you pass these variables in a safer way (that is, without string concatenation)?


Yeah, I'm going to parametrize that and probably use custom DQL functions for that anyway. The code here is what I took from an old system where they built all their queries like that (concatenate strings and send it as plain SQL). Quite a bit of work before me
Time is precious. Waste it wisely.
Crying
Profile Joined February 2011
Bulgaria778 Posts
Last Edited: 2015-09-04 00:43:28
September 04 2015 00:43 GMT
#13215
Anyone here interested in P NP problem? What's your thought on it?
Determination~ Hard Work Surpass NATURAL GENIUS!
delHospital
Profile Blog Joined December 2010
Poland261 Posts
September 04 2015 02:48 GMT
#13216
On September 04 2015 09:43 Crying wrote:
Anyone here interested in P NP problem? What's your thought on it?

There's a lot of unsolved problems in complexity theory. I wonder how many of them we're going to solve before we invent strong AI to do it for us
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
September 04 2015 02:48 GMT
#13217
--- Nuked ---
ZenithM
Profile Joined February 2011
France15952 Posts
Last Edited: 2015-09-04 04:16:59
September 04 2015 04:09 GMT
#13218
On September 04 2015 09:43 Crying wrote:
Anyone here interested in P NP problem? What's your thought on it?

Haha sounds like you had your first class in complexity theory, we're all excited about that at first. After a while, "P vs NP" loses a bit of its charm, unfortunately. The most likely answer is that P != NP, and there are literally hundred of other "likely" results that have been demonstrated "assuming P != NP". And people are not really researching things that assume "P = NP", let's be honest here :D.
The efforts required to research P vs NP aren't actually worth the rewards. Tentative proofs (all for the != case, of course) are always super long (big fat complicated books) and basically uncheckable. The only exciting thing that could happen in research is if someone actually has an algorithm for reduction, and that it can be checked for correctness. The "almost impossible" thing is if someone actually has a program that does that in reasonable time, then you can think about all the sci-fi/thriller/cataclysm shit you can again!

"P vs NP" being unsolvable is likely due to this classes theory being flawed in the first place. Think about it: it's not really relevant to the real world, where anything longer than cubic is already unmanageable ;D.

If you still are interested, watch the Traveling Salesman. We had a lot of fun with my friends watching that a few years back (we were all computer science graduates obviously, don't watch it with people who don't know CS at all, it's just a bad movie in that case :D).
njt7
Profile Joined August 2012
Sweden769 Posts
Last Edited: 2015-09-04 10:12:20
September 04 2015 09:57 GMT
#13219
Nvm thx, just needed to take a step back and think about it for a sec
"All the casters who flamed me ever for anything."
TMG26
Profile Joined July 2012
Portugal2017 Posts
September 04 2015 13:55 GMT
#13220
On September 04 2015 11:48 Nesserev wrote:

Also, the consequences would be massive if P-TIME = NP-TIME, especially for security.



BRING ON THE CHAOS.
Supporter of the situational Blink Dagger on Storm.
Prev 1 659 660 661 662 663 1032 Next
Please log in or register to reply.
Live Events Refresh
Safe House 2
17:00
Round Robin
ZombieGrub351
TKL 258
CranKy Ducklings130
3DClanTV 82
EnkiAlexander 58
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
ZombieGrub351
TKL 258
CosmosSc2 79
Nathanias 42
JuggernautJason34
Codebar 24
UpATreeSC 13
StarCraft: Brood War
Britney 34931
Calm 2629
Dewaltoss 131
Hyun 125
ZZZero.O 123
Dota 2
LuMiX1
Heroes of the Storm
Khaldor349
Other Games
Grubby1589
Beastyqt623
Skadoodle449
Pyrionflax243
ToD163
KnowMe155
Mew2King119
Trikslyr46
rGuardiaN30
fpsfer 0
Organizations
Other Games
gamesdonequick2500
BasetradeTV164
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 19 non-featured ]
StarCraft 2
• printf 60
• HeavenSC 21
• Adnapsc2 13
• Migwel
• AfreecaTV YouTube
• sooper7s
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
StarCraft: Brood War
• Airneanach37
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• Ler81
League of Legends
• Nemesis5691
Other Games
• imaqtpie1889
• Shiphtur305
• tFFMrPink 16
Upcoming Events
Sparkling Tuna Cup
14h 5m
Safe House 2
21h 5m
Monday Night Weeklies
1d 20h
WardiTV Invitational
2 days
WardiTV Invitational
2 days
Tenacious Turtle Tussle
4 days
The PondCast
4 days
WardiTV Invitational
5 days
Online Event
5 days
RSL Revival
6 days
[ Show More ]
RSL Revival
6 days
WardiTV Invitational
6 days
Liquipedia Results

Completed

Acropolis #4 - TS2
WardiTV TLMC #15
HCC Europe

Ongoing

BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
C-Race Season 1
IPSL Winter 2025-26
EC S1
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual

Upcoming

SC4ALL: Brood War
BSL Season 21
BSL 21 Team A
BSL 21 Non-Korean Championship
RSL Offline Finals
RSL Revival: Season 3
Stellar Fest
SC4ALL: StarCraft II
CranK Gathers Season 2: SC II Pro Teams
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.